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

全部评论

相关推荐

05-13 00:41
已编辑
北京邮电大学 Java
理性的杰克刷牛客:ai肯定要有的,最好学一下agent方向加一个智能客服什么的进去,并且多加点什么skill,mcp啥的,另外你现在的项目深度有些浅,这些功能都太简单了,而且也不是真正能扛高并发的实现,没有什么太大的亮点,可以去网上找点更有深度的项目。可以先投一些中小厂,有实习经历以后再去大厂,你现在这个大厂可能机会不大
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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