首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

hdu1047 大数相加有关问题,不如何为什么wa

2013-09-15 
hdu1047 大数相加问题,不怎么为什么wa题目One of the first users of BITs new supercomputer was Chip D

hdu1047 大数相加问题,不怎么为什么wa
题目
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers. 
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.) 
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative). 

The final input line will contain a single zero on a line by itself.
Your program should output the sum of the VeryLongIntegers given in the input. 


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
1


123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

370370367037037036703703703670
代码:
#include<stdio.h>
#include<string.h>
int num0[102][102];
int num1[105];
char str[105];
int main()
{
int n;
int i,j,k,l;
int len;
int maxlen;
int temp;
scanf("%d",&n);
for(i=0;i<n;i++)
{
maxlen=0;
for(j=0;j<105;j++)
num1[j]=0;
j=0;
while(scanf("%s",str),strcmp(str,"0"))
{
len=strlen(str);
if(len>maxlen)
maxlen=len;
for(k=0;k<len;k++)
{
num0[j][len-1-k]=str[k]-'0';


}
j++;
}
temp=0;
for(k=0;k<maxlen;k++)
{
num1[k]+=temp;
for(l=0;l<j;l++)
{
num1[k]+=num0[l][k];
}
temp=num1[k]/10;
num1[k]%=10;
}
while(temp)
{
num1[k]+=temp%10;
temp/=10;
k++;
}
if(i)
printf("\n");
if(j)
{
for(l=k-1;l>=0;l--)
printf("%d",num1[l]);
printf("\n");
}
else
printf("0\n");
}

return 0;
}
[解决办法]
试以下输入:
2

1111
11
0

11
1111
0

第2块的输出错误,原因是没有对num0清0。

加上以下语句可以AC:

memset(num0, 0, sizeof(num0));//增加
while(scanf("%s",str),strcmp(str,"0"))



热点排行