Problem A+B(Big Integer)

/*========================================================================
Problem A+B(Big Integer)
Time Limit:1000MS Memory Limit:65536KB
Total Submit:3205 Accepted:922
Description 
Give two positive integer A and B,calucate A+B.
Notice that A,B is no more than 500 digits.
Input 
The test case contain several lines.Each line contains two positive integer A and B.
Output 
For each input line,output a line contain A+B
Sample Input 
2 3
1231231231823192 123123123123123
1000000000000000 1
Sample Output 
5
1354354354946315
1000000000000001
Source
EOJ
==========================================================================*/

http://202.120.80.191/problem.php?problemid=1001

 1 #include<stdio.h>
 2 #include<string.h>
 3 void sum(char a[],char b[],char c[]);//c=a+b
 4 void swap(char a[]);
 5 int main()
 6 {
 7     char a[503],b[503],c[505];
 8     freopen("5.in","r",stdin);
 9     while(scanf("%s%s",a,b)!=EOF)//while(cin>>a>>b)
10     {
11         sum(a,b,c);
12         printf("%s\n",c);
13     }
14     return 0;
15 }
16 void sum(char a[],char b[],char c[])//c=a+b
17 {
18     int i,lenA,lenB,min,max;
19     int carry=0,t;
20     lenA=strlen(a);
21     lenB=strlen(b);
22     swap(a);
23     swap(b);
24     if(lenA>lenB)
25     {
26         max=lenA;
27         min=lenB;
28     }
29     else
30     {
31         max=lenB;
32         min=lenA;
33     }
34     for(i=0;i<min;i++)
35     {
36         t=(a[i]-'0')+(b[i]-'0')+carry;
37         c[i]=t%10+'0';
38         carry=t/10;
39     }
40     if(lenA>lenB)
41     {
42         for(i=min;i<max;i++)
43         {
44             t=(a[i]-'0')+carry;
45             c[i]=t%10+'0';
46             carry=t/10;
47         }
48     }
49     else
50     {
51         for(i=min;i<max;i++)
52         {
53             t=(b[i]-'0')+carry;
54             c[i]=t%10+'0';
55             carry=t/10;
56         }
57     }
58     if(carry!=0)
59     {
60         c[i]=carry+'0';
61         i++;
62     }
63     c[i]='\0';
64     swap(c);
65 }
66 void swap(char a[])
67 {
68     int i,len=strlen(a),t=len/2;
69     char ch;
70     for(i=0;i<t;i++)
71     {
72         ch=a[i];
73         a[i]=a[len-1-i];
74         a[len-1-i]=ch;
75     }
76 }
View Code

 

 

 

 

posted on 2014-03-02 13:40  华山青竹  阅读(704)  评论(0)    收藏  举报

导航