题解 | #a+b#

a+b

https://www.nowcoder.com/practice/4c39c984ea3848b48e111b8e71ec1dd4

//C++版代码
#include <iostream>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    string a, b;
    while (cin >> a >> b) {
        int max_length = max(a.length(), b.length());
        a = string(max_length - a.length(), '0') + a;
        b = string(max_length - b.length(), '0') + b;
        string result;
        int carry = 0;
        for (int i = max_length - 1; i >= 0; i--) {
            int sum = a[i] - '0' + b[i] - '0' + carry;
            carry = sum / 10;
            result = to_string(sum % 10) + result;
        }
        if (carry) result = to_string(carry) + result;
        cout << result << endl;
    }
    return 0;
}
//Java版代码
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextBigInteger()) {
            BigInteger a = sc.nextBigInteger();
            BigInteger b = sc.nextBigInteger();
            System.out.println(a.add(b));
        }
    }
}
#Python版代码
while True:
    try:
        print(eval(input().replace(' ', '+')))
		#print(sum(map(int, input().split())))
    except:
        break

全部评论

相关推荐

HNU_fsq:建议直接出国,这简历太6了。自愧不如
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务