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
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
using namespace std;///
#define MAX_BIT 100
void toDigit(char *str, int *digit)//把char转为int数组,倒序方便输出
{
int len = strlen(str);
for (int i = 0, j = len - 1; i < len; i++, j--) {
digit[i] = (int)(str[j] - '0');
}
}
void add(int *digitNum,int *rslt) //相加函数,rslt为结果
{
int s = 0;
for (int i = 0; i < MAX_BIT; i++) {
s = rslt[i] + digitNum[i] + s;
rslt[i] = s % 10;
s = s / 10;
}
}
int main()
{
char strNum[MAX_BIT];
int rslt[MAX_BIT], digitNum[MAX_BIT];
memset(rslt, 0, sizeof(rslt));
while (scanf("%s", strNum) && strNum[0] != '0') {
// init
memset(digitNum, 0, sizeof(digitNum));
toDigit(strNum, digitNum);
int s = 0;
for (int i = 0; i < MAX_BIT; i++) {
s = rslt[i] + digitNum[i] + s;
rslt[i] = s % 10;
s = s / 10;
}
}
int i = MAX_BIT;
while (rslt[--i] == 0);
while (i >= 0) {
printf("%d", rslt[i--]);
}
printf("\n");
return 0;
}
网友评论