The "A+B problem" is very easy,but I failed for many times.
The code:
1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 #include<cstdlib> 5 #include<cstring> 6 #include<string> 7 #include<queue> 8 #include<deque> 9 #include<stack> 10 #include<vector> 11 #include<algorithm> 12 #include<iomanip> 13 #define LL long long 14 #define MAXN 50001 15 #define P 123456 16 using namespace std; 17 int a[P],b[P],c[P],ka=1,kb=1; 18 char ca[P],cb[P]; 19 int main() 20 { 21 scanf("%s%s",ca,cb); 22 int lena=strlen(ca),lenb=strlen(cb); 23 int j=1; 24 for(int i=lena-1;i>=0;i--) 25 { 26 a[ka]+=j*(ca[i]-'0'); 27 j*=10; 28 if(j==1000) 29 j=1,ka++; 30 } 31 32 if(j!=1) 33 ka++,j=1; 34 for(int i=lenb-1;i>=0;i--) 35 { 36 b[kb]+=j*(cb[i]-'0'); 37 j*=10; 38 if(j==1000) 39 j=1,kb++; 40 } 41 if(j!=1) 42 kb++,j=1; 43 44 //a[],b[] finish 45 46 int e=0; 47 for(int i=1;i<=max(ka,kb);i++) 48 { 49 c[i]=(a[i]+b[i]+e)%1000; 50 e=(a[i]+b[i]+e)/1000; 51 } 52 int k=max(ka,kb)+1; 53 while(k>0&&!c[k]) k--; 54 printf("%d",c[k]); 55 for(int i=k-1;i>=1;i--) 56 printf("%03d",c[i]); 57 58 return 0; 59 }
I have forgot to plus the "e" first, so I have so much mistake!!!