hdu 1047 Integer Inquiry

http://acm.hdu.edu.cn/showproblem.php?pid=1047  大数相加,没AC 

View Code
 1 #include<iostream>
2 #include<vector>
3 #include<cstring>
4 #include<cstdlib>
5 using namespace std;
6 void add(string a,string b,string &sum)
7 {
8 sum="";
9 int alen=a.length(),blen=b.length();
10 int i=alen-1,j=blen-1,extra=0,x,y,temp;
11 char c;
12 while(i>=0 && j>=0 )
13 {
14 x=a[i]-'0';
15 y=b[j]-'0';
16 temp=x+y+extra;
17 if(temp<10) c=temp+'0',sum=c+sum,extra=0;
18 else
19 {
20 extra=1;
21 c=temp%10+'0';
22 sum=c+sum;
23 }
24 i--;j--;
25 }
26 if(i<0) i=j,a=b;
27 for(;i>=0;i--)
28 {
29 temp=(a[i]-'0')+extra;
30 if(temp<10) extra=0,c=temp+'0',sum=c+sum;
31 else
32 {
33 extra=1;
34 c=temp%10+'0';
35 sum=c+sum;
36 }
37 }
38 if(extra) c=extra+'0',sum=c+sum;
39 //cout<<"sum:"<<sum<<endl;
40 }
41 int main()
42 {
43 int t;
44 cin>>t;
45 while(t--)
46 {
47 string a,b,s;
48 while(cin>>a)
49 {
50 if(a.length()==1 && a[0]=='0') break;
51 s="";
52 add(a,b,s);
53 b=s;
54 }
55 cout<<s<<endl;
56 if(t) cout<<endl;
57 }
58 return 0;
59 }


 

posted @ 2012-04-02 09:20  keepmoving89  阅读(136)  评论(0编辑  收藏  举报