ZOJ Problem Set - 1001
Calculate a + b
Input
The input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.Output
For each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.Sample Input
1 5
Sample Output
6
注:此题为:ZOJ 1001 A + B Problem
说明:此题就是一个普通的a+b问题,不是大数。
已AC源代码:
#include<iostream>
using namespace std;
int main()
{
int a,b;
while(cin>>a>>b)
{
cout<<a+b<<endl;
}
return 0;
}
以下是用大数写的,也可以AC:
#include<iostream>
#include<cstdio>
#include<cstring>
#define M 500
using namespace std;
char ch1[M],ch2[M];
int a[M],b[M];
int main()
{
while(~scanf("%s%s",ch1,ch2))
{
int len,len1,len2,i,j;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
len1=strlen(ch1);
len2=strlen(ch2);
for(i=0,j=len1-1;i<len1;++i,--j)
a[j]=ch1[i]-'0';
for(i=0,j=len2-1;i<len2;++i,--j)
b[j]=ch2[i]-'0';
len=len1>len2?len1:len2;
for(i=0;i<len;++i)
{
a[i]+=b[i];
if(a[i]>=10)
{
a[i+1]++;
a[i]-=10;
}
}
for(i=len+10;i>=0;--i)
if(a[i]!=0)
break;
for(;i>=0;--i)
cout<<a[i];
cout<<endl;
}
return 0;
}