HDU A + B Problem II
题意:两个数相加,计算A+B的和,输出每两组答案之间有个换行。
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <set> 5 #include <vector> 6 #include <map> 7 #define rep(i,a,b) for(int i=(a);i<=(b);++i) 8 #define per(i,a,b) for(int i=(a);i>=(b);--i) 9 using namespace std; 10 typedef long long ll; 11 const int N=1e3+7; 12 string a,b; 13 int t; 14 vector<int>add(vector<int> &A, vector<int> &B) 15 { 16 vector<int>C; 17 int t=0; 18 int La=A.size(),Lb=B.size(); 19 int maxx=max(La,Lb); 20 rep(i,0,maxx-1){ 21 if(i<La) t+=A[i]; 22 if(i<Lb) t+=B[i]; 23 C.push_back(t%10); 24 t/=10; 25 } 26 if(t) C.push_back(1); 27 return C; 28 } 29 30 int main() 31 { 32 scanf(" %d",&t); 33 int ok=t,idx=0; 34 while(t--){ 35 getchar(); 36 cin>>a>>b; 37 vector<int>A,B; 38 idx++; 39 int la=a.size(), lb=b.size(); 40 per(i,la-1,0) A.push_back(a[i]-'0'); 41 per(i,lb-1,0) B.push_back(b[i]-'0'); 42 vector<int> C=add(A,B); 43 std::cout<<"Case "<<idx<<":\n"; 44 std::cout<<a<<" + "<<b<<" = "; 45 int Lc=C.size(); 46 per(i,Lc-1,0) cout<<C[i]; 47 std::cout<<'\n'; 48 if(idx!=ok) std::cout<<'\n'; 49 C.clear();A.clear();B.clear(); 50 } 51 return 0; 52 }
永远年轻 永远热泪盈眶!