题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <iostream>
#include <string>
using namespace std;
int main() {
string input1, input2;
getline(cin, input1);
getline(cin, input2);
string result = "";
if (input1.length() < input2.length()) {
swap(input1, input2);
}//确保input1是长的
int plus = 0, temp = 0;
int i = input1.length()-1,j=input2.length()-1;//从个位开始
while(j>=0) {
temp = (input1[i] - '0') + (input2[j] - '0') + plus;
result.push_back(temp % 10+'0');
plus=temp/10;
j--;
i--;
}//公共部分
while(i>=0){
temp = (input1[i]-'0')+plus;
result.push_back(temp%10+'0');
plus=temp/10;
i--;
}//剩余部分
if (plus) {
result.push_back(plus+'0');
}//最后一个进位
for(int i=result.size()-1;i>=0;i--){
cout<<result[i];
}//反过来输出
}
// 64 位输出请用 printf("%lld")

查看5道真题和解析
海康威视公司福利 1160人发布