寒假训练:Cancer's Trial 一月二十号 第九题

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. “Thissupercomputerisgreat,”remarkedChip. “IonlywishTimothywereheretoseetheseresults.” (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
用结构来记下长度,并把字符串转化为单个数字
#include<stdio.h>
#include
#include
using namespace std;
struct HugeInt
{ int len;
int num[335];
};
HugeInt a, w;
char c[335];
void Scan_HugeInt()
{
cin >> c;
a.len = strlen©;
for (int i = 0; i < a.len; i++)
a.num[a.len - i] = c[i] - ‘0’;
}
void Plus()
{
w.len = (a.len > w.len) ? a.len : w.len;
for (int i = 1; i <= w.len; i++)
{
w.num[i] += a.num[i];
w.num[i + 1] += w.num[i] / 10;
w.num[i] %= 10;
}
if (w.num[w.len + 1] != 0)
w.len++;
}
int main()
{
while (1)
{
Scan_HugeInt();
if (c[0] == ‘0’)
break;
memset(c, 0, sizeof©);
Plus();
}
for (int i = w.len; i >= 1; i–)
cout << w.num[i];
cout << endl;
return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43978403/article/details/86683998