题解 | #数字反转#
数字反转
https://www.nowcoder.com/practice/2687c5c174cb4f938bdae01f0a19490c
#include <iostream> #include <vector> #include <algorithm> using namespace std; vector<int> add(vector<int> &A,vector<int> &B){ int t=0; vector<int> C; for(int i=0;i<A.size()||i<B.size();i++){ if(i<A.size()) t+=A[i]; if(i<B.size()) t+=B[i]; C.push_back(t%10); t/=10; } if(t) C.push_back(t); return C; } int main() { string a,b; while(cin>>a>>b){ vector<int> A,B; for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0');//21 for(int i=b.size()-1;i>=0;i--) B.push_back(b[i]-'0');//43 vector<int> res=add(A,B);//两个数的和:个十百...64 A.clear(); B.clear(); for(int i=0;i<a.size();i++) A.push_back(a[i]-'0');//12 for(int i=0;i<b.size();i++) B.push_back(b[i]-'0');//34 vector<int> res_reverse=add(A,B);//两个数反转的和:46 int sum=0,sum_reverse=0; for(int i=0;i<res.size();i++) sum=10*sum+res[i];//64 for(int i=res_reverse.size()-1;i>=0;i--) sum_reverse=10*sum_reverse+res_reverse[i];//64 if(sum==sum_reverse){ int ans=0; for(int i=res.size();i>=0;i--) ans=10*ans+res[i];//46 cout<<ans<<endl; }else{ cout<<"NO"<<endl; } } return 0; }