Description
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.)
``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.)
Input
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.
The final input line will contain a single zero on a line by itself.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0
Sample Output
370370367037037036703703703670
解题思路:
就是一般的高精度加法题,唯一需要注意的是输入数据中有些会有前导0,数据太狗了!害我WA了好几发!所以不能以字符串第一个是0来判断输入结束。还得判断第二个是否为结束符。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1000;
int sum[maxn];
void Sum(char str[maxn])
{
int length, num[maxn];
memset(num, 0, sizeof(num));
length = strlen(str);
for(int i = 0; i < length; i++)
num[i] = str[length - i - 1] - '0';
for(int i = 0; i < maxn - 1; i++)
{
sum[i] += num[i];
if(sum[i] >= 10)
{
sum[i] %= 10;
sum[i+1]++;
}
}
}
int main()
{
char str[maxn];
memset(sum, 0, sizeof(sum));
while(scanf("%s", str))
{
if(str[0] == '0' && str[1] == 0)
break;
Sum(str);
}
bool isBegin = false;
for(int i = maxn - 1; i >= 0; i--)
{
if(isBegin)
printf("%d",sum[i]);
else if(sum[i])
{
printf("%d",sum[i]);
isBegin = true;
}
}
if(!isBegin)
printf("0");
printf("\n");
return 0;
}