题解 | #Integer Inquiry#
Integer Inquiry
https://www.nowcoder.com/practice/df03906024d742cc919a64c22983f5e1
#include <iostream> #include <vector> using namespace std; vector<int> add(vector<int> &A,vector<int> &B){ vector<int> C; int t=0; for(int i=0;i<A.size()||i<B.size();i++){ if(i<A.size()) t+=A[i]; if(t<B.size()) t+=B[i]; C.push_back(t%10); t/=10; } if(t) C.push_back(t);//最高位进位 //去除前导零 while(C.size()>1&&C.back()==0) C.pop_back(); return C; } int main() { vector<int> A; string str; while(cin>>str&&str!="0"){ vector<int> B; for(int i=str.size()-1;i>=0;i--) B.push_back(str[i]-'0');//个十百 A=add(A,B); } for(int i=A.size()-1;i>=0;i--) cout<<A[i]; cout<<endl; return 0; }