您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: