目录
? 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.)
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. 注意输入数据中,VeryLongInteger 可能有前导0
Your program should output the sum of the VeryLongIntegers given in the input.
输入:
123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0输出:
370370367037037036703703703670
AC代码~
#include<stdio.h>
#include<string.h>
int main()
{
char str1[101]; // 将str1作为中间结果
char str2[101];
int ans[101]={0}; // 中间结果的int表示
scanf("%s",str1);
while(scanf("%s",str2) != EOF)
{
if(!strcmp(str2,"0"))
break;
else
{
int num1[101]={0};
int num2[101]={0};
int n1 = strlen(str1);
int n2 = strlen(str2);
//将str1和str2从低位到高位存入num1和num2
for(int i = 0;i<n1;i++)
num1[i] = str1[n1-1-i] - '0';
for(int i = 0;i<n2;i++)
num2[i] = str2[n2-1-i] - '0';
int n = n1>n2?n1:n2;
int c = 0;
for(int i = 0;i<n;i++)
{
ans[i] = (num1[i] +num2[i] + c) %10;
c = (num1[i] +num2[i] + c) /10;
}
if(c) // 最高位有进位
{
ans[n] = c;
n++;
}
for(int i = 0;i<n;i++)
str1[i] = ans[n-1-i]+'0';
}
}
puts(str1);
return 0;
}