首页 > 其他 > 详细

Problem A+B(Big Integer)

时间:2014-03-03 11:20:24      阅读:729      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
/*========================================================================
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
==========================================================================*/
bubuko.com,布布扣

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

bubuko.com,布布扣
 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

Problem A+B(Big Integer),布布扣,bubuko.com

Problem A+B(Big Integer)

原文:http://www.cnblogs.com/huashanqingzhu/p/3576725.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!