poj1503(大数相加)

来源:互联网 发布:win10关闭软件快捷键 编辑:程序博客网 时间:2024/05/29 08:08

http://poj.org/problem?id=1503

Integer Inquiry
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 24392Accepted: 9480

Description

One of the first users of BIT'snew supercomputer was Chip Diller. He extended his exploration ofpowers of 3 to go from 0 to 333 and he explored taking various sumsof those numbers.
``This supercomputer is great,'' remarked Chip. ``I only wishTimothy were here to see these results.'' (Chip moved to a newapartment, once one became available on the third floor of theLemon Sky apartments on Third Street.)

Input

The input will consist of atmost 100 lines of text, each of which contains a singleVeryLongInteger. Each VeryLongInteger will be 100 or fewercharacters in length, and will only contain digits (noVeryLongInteger will be negative).

The final input line will contain a single zero on a line byitself.

Output

Your program should output thesum of the VeryLongIntegers given in the input.

Sample Input

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

Sample Output

370370367037037036703703703670

Source

East Central North America 1996
模拟大数相加。。
#include<stdio.h>
#include<string.h>
#define MAXN 1100
chars[MAXN+10];//数组开大点吧
intnum[MAXN+10];
int main()
{
memset(num,0,sizeof(num));
memset(s,0,sizeof(s));
int i,j;
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"0")==0)
break;
intlen=strlen(s);
for(i=0;i<len;i++)
{
num[i]+=s[len-i-1]-'0';
//if(num[i]>9)
// {
// num[i]%=10;
// num[i+1]+=1;
// }
}
}
for(i=0;i<MAXN;i++)
{
while(num[i]>=10)
{
num[i+1]+=num[i]/10;
num[i]=num[i]%10;
}
}
for(i=MAXN;i>=0;i--)//去除前导0
{
if(num[i]!=0)
break;
}
for(j=i;j>=0;j--)
{
printf("%d",num[j]);
}
printf("\n");
return 0;
}
原创粉丝点击