HDOJ 1002

A + B Problem II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 124589    Accepted Submission(s): 23993


ios

Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
 

 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
 

 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
 

 

Sample Input
2 1 2 112233445566778899 998877665544332211
 

 

Sample Output
Case 1: 1 + 2 = 3 Case 2: 112233445566778899 + 998877665544332211 = 1111111111111111110
 

 

 1 #include <iostream>
 2 using namespace std;  3 
 4 void sum(char (&a)[1001],char (&b)[1001]);  5 int main()  6 {  7     int T, count_T;  8     char a[1001], b[1001];  9     cin >> T; 10     for(count_T=1; count_T <= T; count_T++) 11  { 12         cin >> a >> b; 13 
14         if(count_T==1) 15  { 16             cout << "Case " << count_T << ":" << endl; 17  } 18         else 
19  { 20             cout << "\nCase " << count_T << ":" << endl; 21  } 22 
23         cout << a << " + " << b << " = "; 24 
25  sum(a,b); 26  } 27     return(0); 28 } 29 void sum(char (&a)[1001],char (&b)[1001]) 30 { 31     char c[1002]; 32     int la, lb; 33     la=strlen(a)-1; 34     lb=strlen(b)-1; 35     int i; 36     int flag=0; 37     for(i=0;la>=0 && lb>=0;la--,lb--,i++) 38  { 39         flag+=(a[la]-'0')+(b[lb]-'0'); 40         c[i]=flag%10; 41         flag/=10; 42  } 43     for(la=la;la>=0;la--,i++) 44  { 45         flag+=(a[la]-'0'); 46         c[i]=flag%10; 47         flag/=10; 48  } 49     for(lb=lb;lb>=0;lb--,i++) 50  { 51         flag+=(b[lb]-'0'); 52         c[i]=flag%10; 53         flag/=10; 54  } 55     if(flag) 56  { 57         c[i]=flag; 58         i++; 59  } 60     for(i--;i>=0;i--) 61  { 62         cout<<(char)(c[i]+'0'); 63  } 64     cout<<endl; 65 }
相關文章
相關標籤/搜索