非常简单的大数加法,因为不会Java只能手写大数加法了;博客存一下以后回来看看
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1000+5; 4 char A[maxn],B[maxn],ans[maxn]; 5 void flip(char str[]) 6 { 7 int startstr=0,endstr=strlen(str)-1; 8 for(;startstr<endstr;startstr++,endstr--) 9 swap(str[startstr],str[endstr]); 10 } 11 void slove() 12 { 13 flip(A),flip(B); 14 int last = 0 ; 15 int len = max(strlen(A),strlen(B)); 16 for(int i=0;i<len;i++) 17 { 18 int tmp = last; 19 if(A[i]) tmp += A[i] -'0'; 20 if(B[i]) tmp += B[i] -'0'; 21 last = tmp / 10; 22 ans[i] = tmp%10 + '0'; 23 } 24 if(last) ans[len] = last + '0' ; 25 flip(ans);flip(A);flip(B); 26 } 27 int main() 28 { 29 int n;cin >> n; 30 for(int i=1;i<=n;i++) 31 { 32 memset(A,0,sizeof(A)); 33 memset(B,0,sizeof(B)); 34 memset(ans,0,sizeof(ans)); 35 cin >> A >> B; 36 if(i!=1) cout << " "; 37 printf("Case %d: ",i); 38 slove(); 39 printf("%s + %s = %s ",A,B,ans); 40 } 41 return 0; 42 }