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.)
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.
Output Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900
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;}