Problem A+B(Big Integer)
2014-03-02 13:40
267 查看
/*======================================================================== 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
#include<stdio.h> #include<string.h> void sum(char a[],char b[],char c[]);//c=a+b void swap(char a[]); int main() { char a[503],b[503],c[505]; freopen("5.in","r",stdin); while(scanf("%s%s",a,b)!=EOF)//while(cin>>a>>b) { sum(a,b,c); printf("%s\n",c); } return 0; } void sum(char a[],char b[],char c[])//c=a+b { int i,lenA,lenB,min,max; int carry=0,t; lenA=strlen(a); lenB=strlen(b); swap(a); swap(b); if(lenA>lenB) { max=lenA; min=lenB; } else { max=lenB; min=lenA; } for(i=0;i<min;i++) { t=(a[i]-'0')+(b[i]-'0')+carry; c[i]=t%10+'0'; carry=t/10; } if(lenA>lenB) { for(i=min;i<max;i++) { t=(a[i]-'0')+carry; c[i]=t%10+'0'; carry=t/10; } } else { for(i=min;i<max;i++) { t=(b[i]-'0')+carry; c[i]=t%10+'0'; carry=t/10; } } if(carry!=0) { c[i]=carry+'0'; i++; } c[i]='\0'; swap(c); } void swap(char a[]) { int i,len=strlen(a),t=len/2; char ch; for(i=0;i<t;i++) { ch=a[i]; a[i]=a[len-1-i]; a[len-1-i]=ch; } }
View Code
相关文章推荐
- Problem A+B(Big Integer)大数相加
- A * B Problem (Big integer version) 【高精度】
- A + B Problem (Big integer versi…
- A * B Problem (Big integer versi…
- ECNU-ACM-Problem A+B(Big Integer)
- A * B Problem (Big integer version) 【高精度】
- A + B Problem (Big integer version) 【高精度】
- http://acm.hdu.edu.cn/showproblem.php?pid=1108 java BIgInteger gcd
- 【爱上Java8】BigInteger在Java8中的改进
- JAVA之BigInteger
- C++ 实现Biginteger
- java.math.BigInteger使用心得总结
- bitVector@ java bit自我practice##Q&A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略
- SZU:B47 Big Integer II
- 【java.math.BigInteger】【转】常见问题
- 1005 大数加法 ——51Nod(java BigInteger)
- biginteger模版,没有压位(较精简)
- Java中BigInteger详解
- BigInteger之高精度乘法
- 采用BigInteger进行的权限设计