题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <iostream>
using namespace std;
string bigIntAdd(string s1,string s2){
string res;
int i=s1.size()-1,j=s2.size()-1;
int add,carry = 0;
while (i>=0 && j>=0) {
add = (s1[i--]-'0') + (s2[j--]-'0') + carry;
carry = add / 10;
res = to_string(add%10) + res;
}
while (i>=0){
add = (s1[i--]-'0') + carry;
carry = add / 10;
res = to_string(add%10) + res;
}
while (j>=0){
add = (s2[j--]-'0') + carry;
carry = add / 10;
res = to_string(add%10) + res;
}
while (carry){
res = to_string(carry) + res;
carry = 0;
}
return res;
}
int main(){
string s1,s2;
while (cin>>s1>>s2){
cout<<bigIntAdd(s1,s2)<<endl;
}
return 0;
}
查看5道真题和解析
智元机器人成长空间 174人发布