杭电1002 A+Bproblem。。。交了七遍,最后发现原来有五遍是因为没删测试输出= =、、

 

A + B Problem II

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

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. int main() 
  4.     int n,i,l1,l2,max,a,j,t; 
  5.     char s1[1001],s2[1001],s3[1001]; 
  6.     scanf("%d",&n); 
  7.     for(i = 0;i < n;i++) 
  8.     { 
  9.         scanf("%s%s",s1,s2); 
  10.  
  11.         l1 = strlen(s1); 
  12.         l2 = strlen(s2); 
  13.         if(l1>l2) 
  14.             max = l1; 
  15.         else 
  16.             max = l2; 
  17.  
  18.         t=max; 
  19.         memset(s3,'\0',1001); 
  20.         memset(s3,'0',max+1); 
  21.         l1 = l1-1; 
  22.         l2 = l2-1; 
  23.         max = max; 
  24.         s3[0]='0'
  25.         while(l1>=0 && l2>=0) 
  26.         { 
  27.             a = s1[l1--]-'0'+s2[l2--]-'0'+s3[max]-'0';//问题可以直接简化为小学的算术题,从后往前算 
  28.             s3[max--] ='0'+a%10; 
  29.             if(a>=10) 
  30.                 s3[max]+=1;//大于十的时候记得向前加一,因为以上三个数永远不会大于等于20.。。。 
  31.              
  32.         } 
  33.         while(l1>=0) 
  34.         { 
  35.             a = s3[max]-'0'+s1[l1--]-'0'
  36.             s3[max--]='0'+a%10; 
  37.             if(a>=10) 
  38.                 s3[max]+=1; 
  39.         } 
  40.         while(l2>=0) 
  41.         { 
  42.             a = s3[max]-'0'+s2[l2--]-'0'
  43.             s3[max--]='0'+a%10; 
  44.             if(a>=10) 
  45.                 s3[max]+=1; 
  46.         } 
  47.  
  48.         printf("Case %d:\n",i+1); 
  49.         printf("%s + %s = ",s1,s2); 
  50.  
  51.         if(s3[0] > '0'
  52.             printf("%c",s3[0]);//这一步很重要,假设是1和9想加,会上升一位 
  53.         for(j = 1;j < t; j++) 
  54.             printf( "%c", s3[j]); 
  55.         printf( "%c\n", s3[j]); 
  56.         if(i<n-1) 
  57.             printf("\n"); 
  58.     } 
  59.     return 0; 

 

你可能感兴趣的:(职场,休闲,字符串水题)