您的位置:首页 > 产品设计 > UI/UE

△UVA424 - Integer Inquiry(大数相加)

2014-04-24 19:47 399 查看
IntegerInquiry
OneofthefirstusersofBIT'snewsupercomputerwasChipDiller.Heextendedhisexplorationofpowersof3togofrom0to333andheexploredtakingvarioussumsofthosenumbers.

``Thissupercomputerisgreat,''remarkedChip.``IonlywishTimothywereheretoseetheseresults.''(Chipmovedtoanewapartment,onceonebecameavailableonthethirdflooroftheLemonSkyapartmentsonThirdStreet.)

Input

Theinputwillconsistofatmost100linesoftext,eachofwhichcontainsasingleVeryLongInteger.EachVeryLongIntegerwillbe100orfewercharactersinlength,andwillonlycontaindigits(noVeryLongIntegerwillbenegative).

Thefinalinputlinewillcontainasinglezeroonalinebyitself.

Output

YourprogramshouldoutputthesumoftheVeryLongIntegersgivenintheinput.

SampleInput

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0


SampleOutput

370370367037037036703703703670

自己写的太麻烦了,学习了别人的代码。


#include<cstdio>
#include<string.h>

#definemaxn1005

intsum[maxn];

voidadd(char*str)
{
inti,j,len;
len=strlen(str);
for(i=maxn-1,j=len-1;j>=0;i--,j--)//sum[maxn-1]存的是个位,sum[maxn-2]存的是十位……
{
sum[i]+=str[j]-'0';//字符串,别忘了-'0'
}
for(i=maxn-1;i>0;i--)
{
if(sum[i]>9)//需要进位
{
sum[i]-=10;
sum[i-1]++;
}
}
}

intmain()
{
chars[maxn];
inti;
memset(sum,0,sizeof(sum));
while(gets(s)!=0)
{
if(strcmp(s,"0")==0)
break;
add(s);
}
for(i=0;i<maxn;i++)
{
if(sum[i])//忽略前导0
break;
}
for(;i<maxn;i++)
{
printf("%d",sum[i]);
}
printf("\n");
return0;
}



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