Integer Inquiry
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 25660 | Accepted: 9972 |
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
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
//strlen根据ascii的0来判断结束,需要留一位来做结束标记
char input[101];
char sum[102];
//输出初始化为0
memset(sum,48,sizeof(sum));
while(1)
{
memset(input,0,sizeof(input));
cin>>input;
//"0"表示字符串0,数字48
if(strcmp(input,"0")==0)
break;
int length = strlen(input);
int i;
for(i=0;i<length;i++)
{
sum[101-i]+= input[length-i-1]-48;
//'9'表示字符9,大于9就进一位
if(sum[101-i]>'9')
{
sum[100-i] = sum[100-i]+1;
sum[101-i] = sum[101-i]-10;
}
}
}
//找到字符串不为0的起始位
int flag;
for(int i=0;i<102;i++)
{
if(sum[i]!='0')
{
flag = i;
break;
}
}
for(;flag<102;flag++)
cout<<sum[flag];
cout<<endl;
return 0;
}