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

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");
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_37131037/article/details/83314280