POJ1503: Integer Inquiry(連續多個大整數加法運算),poj1503inquiry
1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 string sum;
5 const int max_len = 110;
6 string tool(string a){
7 int dif = max_len - a.length();
8 string s = "";
9 for(int i=0;i<dif;i++){
10 s += '0';
11 }
12 s+=a;
13 return s;
14 }
15 void solve(string a){
16 a = tool(a);
17 sum = tool(sum);
18 string ssum;
19 ssum = tool(ssum);
20
21 for(int i=max_len-1;i>=1;i--){
22 int temp = (sum[i] -'0') + (a[i] - '0');
23 int c = ssum[i]-'0'+temp;
24 ssum[i] = c%10 + '0';
25 ssum[i-1] = (ssum[i-1]-'0'+c/10) + '0';
26 }
27
28 sum = ssum;
29 }
30 int main(){
31 string a; sum = "";
32 while(cin>>a && a!="0"){
33 solve(a);
34 }
35 int i;
36 for(i=0;i<max_len;i++){
37 if(sum[i]!='0') break;
38 }
39 for(int j=i;j<max_len;j++){
40 cout<<sum[j];
41 }cout<<endl;
42 }
View Code