Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35421 | Accepted: 13811 |
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
Source
Regionals 1996 >> North America - East Central NA
问题链接:POJ1503 UVA424 UVALive5399 Integer Inquiry
问题简述:(略)
问题分析:一个简单的大数加法问题。
程序说明:
用数组来模拟大数计算。
读入的数据放在字符串数组中,是从左到右存储的,高位存储在下标小的元素里。而加法运算是从低位到高位进行的。计算的结果则先输出高位。
本程序采用各个位先都加起来,然后再处理进位的方式,所以数组ans[]的类型使用int。
需要注意的是要去掉高位的0。
题记:(略)
参考链接:(略)
AC的C++语言程序如下:
/* POJ1503 UVA424 UVALive5399 Integer Inquiry */
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N = 100;
char s[N + 1];
int ans[N + 2];
int main()
{
memset(ans, 0, sizeof(ans));
while(~scanf("%s", s) && strcmp(s, "0") != 0) {
int l = strlen(s) - 1;
int j = 0;
for(int i=l; i>=0; i--)
ans[j++] += s[i] - '0';
}
int len;
for(int i=N-1; i>=0; i--)
if(ans[i]) {
len = i;
break;
}
int i = 0;
while(i <= len){
if(ans[i] >= 10) {
ans[i+1] += ans[i] / 10;
ans[i] %=10;
}
i++;
}
while(ans[len+1] != 0) {
len++;
if(ans[len-1] > 10) {
ans[len] += ans[len - 1] / 10;
ans[len - 1] %= 10;
}
}
for(int i=len; i>=0; i--)
printf("%d", ans[i]);
putchar('\n');
}