题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
C++简单易懂,先反转从最低位数开始处理数据,设立标志位大于10,flag=1,反之flag=0; #include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string s1; string s2; cin>>s1>>s2; reverse(s1.begin(), s1.end()); reverse(s2.begin(), s2.end()); int flag=0; int idx=0; string ans=""; while(idx<s1.size() || idx<s2.size()){ int n1=idx>=s1.size()?0:s1[idx]-'0'; int n2=idx>=s2.size()?0:s2[idx]-'0'; if(n1+n2+flag<10){ ans.push_back(char('0'+n1+n2+flag)); flag=0; } else{ ans.push_back(char('0'+(n1+n2+flag)%10)); flag=1; } idx++; } if(flag) ans.push_back('1'); reverse(ans.begin(), ans.end()); cout<<ans<<endl; return 0; }