1065 A+B and C (64bit) (20 分)

本文提供了一道名为1065A+BandC的编程题的解决方案,分别用C++和Java两种语言实现。C++版通过判断长整型数相加可能引起的溢出来确定结果是否大于C,而Java版则利用BigInteger类进行大数运算,确保了计算的准确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1065 A+B and C (64bit) (20 分)

代码

c++版
#include <bits/stdc++.h>
using namespace std;
int main() {
	long long a, b, c, sum;
	int n;
	cin>>n;
	for (int i = 0; i < n; i++) {
		cin>>a>>b>>c;
		sum = a+b;
		if (a>0 && b>0 && sum<=0) {//两个很大的正数相加溢出为负
			cout<<"Case #"<<i+1<<": true"<<endl;
		} else if (a<0 && b<0 && sum>=0) {//两个很小的数溢出为正
			cout<<"Case #"<<i+1<<": false"<<endl;
		} else if (sum > c) {
			cout<<"Case #"<<i+1<<": true"<<endl;
		} else {
			cout<<"Case #"<<i+1<<": false"<<endl;
		}
		
	}
	return 0;
}
java版
import java.math.BigInteger;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		BigInteger a, b, c;
		int n = scan.nextInt();
		for (int i = 0; i < n; i++) {
			a = scan.nextBigInteger();
			b = scan.nextBigInteger();
			c = scan.nextBigInteger();
			if (a.add(b).compareTo(c) == 1) {
				System.out.println("Case #"+(i+1)+": true");
			} else {
				System.out.println("Case #"+(i+1)+": false");
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值