hdu 1002 A + B Problem II 解题报告

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002

                 A + B Problem II  
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 < stdio.h >
2 #include < string .h >
3   #define Max 10000
4 char sun[Max],str1[Max],str2[Max];
5 void jia( )
6 {
7 memset( sun, 0 , sizeof ( sun ) );
8 int len1 = strlen( str1 ),len2 = strlen( str2 );
9 for ( int i = 0 ; i < ( len1 > len2 ? len1 : len2 ); ++ i ) // 转换成ASCII码
10 str1[i] -= ' 0 ' ,str2[i] -= ' 0 ' ;
11 for ( int p = 0 , q = len1 - 1 ; p < q; ++ p , -- q ) // 逆序
12 {
13 char c = str1[q];
14 str1[q] = str1[p];
15 str1[p] = c;
16 }
17 for ( int p = 0 , q = len2 - 1 ; p < q; ++ p, -- q)
18 {
19 char c = str2[q];
20 str2[q] = str2[p];
21 str2[p] = c;
22 }
23 for ( int i = 0 ,c = 0 ; i < ( len1 > len2 ? len1 : len2 ) || c; ++ i ) // 相加 进位
24 {
25 if ( i < len1 )
26 c += str1[i];
27 if ( i < len2 )
28 c += str2[i];
29 sun[i] = c % 10 ; // 不同的地方1
30 c /= 10 ; // 不同的地方2
31 }
32 }
33 int main(){
34 int T;
35 scanf( " %d " , & T );
36 int m = T;
37 while ( T -- ){
38 scanf( " %s%s " ,str1,str2 );
39 char s1[Max],s2[Max];
40 strcpy( s1,str1 );
41 strcpy( s2,str2 );
42 jia( );
43 printf( " Case %d:\n " ,m - T );
44 printf( " %s + %s = " ,s1,s2 );
45 int n = Max;
46 while ( ! sun[ -- n] );
47 for ( ; n >= 0 ; -- n )
48 printf( " %d " ,sun[n]); // 不同的地方3
49 puts( "" );
50 if (T > 0 )
51 puts( "" );
52 }
53 }

你可能感兴趣的:(HDU)