Problem A+B(Big Integer)

简介: /*======================================================================== Problem A+B(Big Integer) Time Limit:1000MS Memory Limit:65536KB Total ...
/*========================================================================
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

 

 

 

 

相关文章
|
8月前
|
人工智能
Big Water Problem
Big Water Problem
44 0
|
Java 应用服务中间件
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
【异常】The field file exceeds its maximum permitted size of 1048576 bytes.
179 0
CF1550B Maximum Cost Deletion(分段比较)
CF1550B Maximum Cost Deletion(分段比较)
46 0
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
Optional int parameter ‘id‘ is present but cannot be translated into a null value due to being ……
328 0
|
算法
LeetCode 321. Create Maximum Number
给定长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,表示两个自然数各位上的数字。现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中取出的数字保持其在原数组中的相对顺序。 求满足该条件的最大数。结果返回一个表示该最大数的长度为 k 的数组。
80 0
LeetCode 321. Create Maximum Number
|
前端开发
Google Earth Engine——Layer error: Description length exceeds maximum.解决办法
Google Earth Engine——Layer error: Description length exceeds maximum.解决办法
679 0
Google Earth Engine——Layer error: Description length exceeds maximum.解决办法
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“
成功解决lightgbm.basic.LightGBMError: Parameter max_depth should be of type int, got “0.02“