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

 

 

 

 

相关文章
|
算法
LeetCode 260. Single Number III
给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。
89 0
LeetCode 260. Single Number III
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
71 0
LeetCode 318. Maximum Product of Word Lengths
|
算法
LeetCode 268. Missing Number
给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
81 0
LeetCode 268. Missing Number
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
Data Structures and Algorithms (English) - 7-12 How Long Does It Take(25 分)
104 0
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)
135 0
|
前端开发
HDOJ 1212 Big Number
HDOJ 1212 Big Number
103 0
HDOJ1018Big Number
HDOJ1018Big Number
103 0
|
XML JavaScript 前端开发