Integer Inquiry
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25671 Accepted Submission(s): 7337
Problem 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.
This problem contains multiple test cases!
The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.
The output format consists of N output blocks. There is a blank line between output blocks.
Sample Input
1
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
Source
East Central North America 1996
这道题挺简单的,复习一下高精度加法,,,hdu的题 格式能再恶心一点吗。次次死在格式上。。。
/*
我寻思着是 大数加法……
*/
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <iomanip>
#include <cstdio>
#define ll long long
#define Max 1110
#define inf 0x3f3f3f3f
using namespace std;
int main() {
ios::sync_with_stdio(false);
int n;
cin>>n;
while(n--) {
int sum[Max]= {0};
string s;
int lensum=0,lens,index;
while(cin>>s) {
index=0;
if(s=="0") break;
lens=s.length();
for(int j=lens-1; j>=0; j--) {
if(s[j]>='0'&&s[j]<='9')
sum[index++]+=(s[j]-'0');//加
}
//处理进位
for(int k=0; k<index; k++) {
if(k!=index-1) {
if(sum[k]>=10) {
sum[k+1]+=sum[k]/10;
sum[k]%=10;
}
} else {
if(sum[k]>=10) {
sum[k+1]+=sum[k]/10;
sum[k]%=10;
index++;
}
}
}
lensum=max(lensum,index);
}
int flag=0;
for(int k=lensum-1; k>=0; k--) {
if(sum[k]) {
flag=1;
}
if(flag) cout<<sum[k];
}
if(flag==0) {//"0"
if(n) {
cout<<"0"<<endl<<endl;
} else cout<<"0"<<endl;
} else {//不是0
if(n) {
cout<<endl<<endl;
} else cout<<endl;
}
}
return 0;
}