Output
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
题目大意是:把各个整数加和,直到0结束
花了不少功夫才AC。主要原因是:输入的字符串有可能有前导零,这点最开始没有考虑进去
#include
#include
using namespace std;
void add(int a[],int & lena,int b[],int & lenb)//把加和的结果保存到a中,并记住lena;
{
int res[10001];
memset(res,0,sizeof(res));
int i;
int len=(lena>=lenb)?(lena+1):(lenb+1);
int ia=0,ib=0;
for (i=1;i=len)
{
res[i]+=a[ia];
ia++;
}
if (i+lenb>=len)
{
res[i]+=b[ib];
ib++;
}
}
for (i=len-1;i>=0;i--)//process the carry;
{
if (res[i]>9)
{
res[i-1]+=res[i]/10;
res[i]=res[i]%10;
}
}
i=0;
while(res[i]==0)//skip the precursor zero;
{
i++;
}
lena=len-i;
for (int j=0;i>s;
if (s[0]==0)
{
cout<<"0"<>s;
while(strcmp(s,"0")!=0)//the mark of the end is "0";
{
i=0;
lenb=0;
while(s[i]!=0)
{
lenb++;
numb[i]=s[i]-'0';
i++;
}
add(numa,lena,numb,lenb);
memset(s,0,sizeof(s));
cin>>s;
}
for (i=0;i