今天開始刷杭電ACM的題庫,我是一隻小笨鳥,只有勤奮才能飛得高,前輩的指點少而精,我會所有作到!ios
#include <iostream> using namespace std; void main() { int a,b; while(cin >> a >> b) cout << a+b << endl; }
#include <iostream> using namespace std; void main() { int n,i,sum; while(cin >> n) { sum=0; for(i=1;i<=n;i++) sum +=i; cout << sum << '\n' << endl; } }
#include <iostream> #include <string> using namespace std; string addstring(string a, string b) { string c=""; int i,j,tmp; i=a.length()-1; j=b.length()-1; tmp=0; while(i>=0 || j>=0) { if(i>=0 && j>=0) tmp += a[i]-'0'+b[j]-'0'; else if(i>=0 && j<0) tmp += a[i]-'0'; else if(i<0 && j>=0) tmp += b[j]-'0'; c = (char)((tmp%10)+'0') + c; tmp /=10; i--; j--; } return c; } int main() { int T,i; string A,B,C; cin >> T; for(i=1; i<=T; i++) { cin >> A >> B; C=addstring(A,B); if(i>1) cout << endl; cout << "Case " << i << ":" << endl; cout << A << " + " << B << " = " << C << endl; } return 0; }
#include <iostream> using namespace std; void the3n1Problem(); int main() { the3n1Problem(); return 0; } #define MAXNUM 1000000 int cache[MAXNUM]; int CycleLength(unsigned long n) { if (n <= 1) return 1; if (n >= MAXNUM) { int m = 0; while(n >= MAXNUM) { if (n%2 == 0) n = n / 2; else n = 3*n + 1; m++; } return CycleLength(n)+m; } if (cache[n] == 0) { if (n%2 == 0) cache[n] = CycleLength(n/2)+1; else cache[n] = CycleLength(3*n+1)+1; } return cache[n]; } int MaxCycleLength(int i, int j) { static bool needInit = true; int k, m; if(i > j) return MaxCycleLength(j,i); if(needInit) { needInit = false; memset(cache,0,sizeof(cache)); for(k = 1, m = 1; k < MAXNUM; k <<= 1, m++) cache[k] = m; } m = 0; for (k = i; k <= j; k++) { int len = CycleLength(k); if (len > m) m = len; } return m; } void the3n1Problem() { int i, j, m; while(cin >> i >> j) { if(i == 0 && j == 0) break; m = MaxCycleLength(i,j); cout << i << ' ' << j << ' ' << m << endl; } }今天就是這麼多了,晚上繼續刷題!算法尚未掌握,書在路上,這幾天快開學了事也多,先本身試着作題吧!加油加油!