题解 | #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

全部评论

相关推荐

2025-11-24 18:53
郑州轻工业大学 Java
点赞 评论 收藏
分享
Tom哥981:这份简历是“大一新生硬凹资深后端”的典型反面教材,槽点离谱到能让面试官直接笑出声: ### 1. 「年龄+入学时间」和项目复杂度完全脱节,可信度直接归0 你2024年7月才入学(现在刚读了1年多),19岁的大一新生,能把Vue3+Spring Boot+ShardingSphere+K8s+AI这些技术全塞进两个项目里?别说实际开发,光把这些技术的文档看完都得半年——这不是“能力强”,是“把招聘JD里的技术词全抄过来造假”,明摆着没碰过实际代码
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务