POJ 2845
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #define MAXN 350 5 using namespace std; 6 7 int _m[MAXN]; 8 9 int main() 10 { 11 //freopen("acm.acm","r",stdin); 12 int n; 13 string s_1; 14 string s_2; 15 int len_1; 16 int len_2; 17 int min; 18 int i; 19 int j; 20 int time = 0; 21 bool boo; 22 cin>>n; 23 24 while(n --) 25 { 26 27 cin>>s_1>>s_2; 28 boo = false; 29 len_1 = s_1.length(); 30 len_2 = s_2.length(); 31 if(len_1 > len_2) 32 { 33 iter_swap(&s_1,&s_2); 34 } 35 36 min = s_2.length() - s_1.length(); 37 for(i = 0; i < min; ++ i) 38 { 39 _m[i] = s_2[i] - '0'; 40 } 41 j = 0; 42 for(i = min; i < s_2.length(); ++ i) 43 { 44 _m[i] = (s_2[i] - '0') + (s_1[j] - '0'); 45 ++ j; 46 } 47 48 int tem = 0; 49 50 for(i = s_2.length() - 1; i >= 0; -- i) 51 { 52 _m[i] += tem; 53 tem = _m[i]/2; 54 _m[i] %= 2; 55 } 56 cout<<++time<<" "; 57 if(tem != 0) 58 { 59 cout<<tem; 60 boo = true; 61 } 62 i = 0; 63 if(tem == 0) 64 { 65 while(_m[i] == 0) 66 { 67 ++ i; 68 } 69 } 70 for(; i < s_2.length(); ++ i) 71 { 72 73 cout<<_m[i]; 74 boo = true; 75 } 76 if(!boo) 77 { 78 cout<<0; 79 } 80 cout<<endl; 81 } 82 }