2013资格赛——Another A+B

来源:互联网 发布:邯郸软件开发 编辑:程序博客网 时间:2024/05/22 10:22

Description

Calculate a + b.

Input

The input will consist of a series of pairs of integers a and b(0<=a,b<=10^1000),separated by a space, one pair of integers per line.

Output

For each pair of input integers a and b you should output the result of a + b in one line.

Sample Input

2 3

Sample Output

5


分析:大数问题。注意细节,边界进位处理。


#include<stdio.h>#include<string.h>int main() {     int n,i,l1,l2,max,a,j,t;     char s1[1001],s2[1001],s3[1001];     while(~scanf("%s%s",s1,s2))         {                 getchar();         l1 = strlen(s1);         l2 = strlen(s2);         if(l1>l2)             max = l1;         else             max = l2;         t=max;         memset(s3,'\0',1001);         memset(s3,'0',max+1);         l1 = l1-1;         l2 = l2-1;         s3[0]='0';         while(l1>=0 && l2>=0)         {             a = s1[l1--]-'0'+s2[l2--]-'0'+s3[max]-'0';             s3[max--] ='0'+a%10;             if(a>=10)                 s3[max]+=1;         }         while(l1>=0)         {             a = s3[max]-'0'+s1[l1--]-'0';             s3[max--]='0'+a%10;             if(a>=10)                 s3[max]+=1;         }         while(l2>=0)         {             a = s3[max]-'0'+s2[l2--]-'0';             s3[max--]='0'+a%10;             if(a>=10)                 s3[max]+=1;         }         if(s3[0] > '0')             printf("%c",s3[0]);         for(j = 1;j < t; j++)             printf( "%c", s3[j]);         printf( "%c\n", s3[j]);         if(i<n-1)             printf("\n");     }     return 0; }