PAT:A1065 A+B and C (64bit) (20 分)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Ecloss/article/details/82664603

PAT:A1065 A+B and C (64bit) (20 分)

Given three integers A, B and C in [−2​63​​,2​63​​], you are supposed to tell whether A+B>C.

Input Specification:

The first line of the input gives the positive number of test cases, T (≤10). Then T test cases follow, each consists of a single line containing three integers A, B and C, separated by single spaces.

Output Specification:

For each test case, output in one line Case #X: true if A+B>C, or Case #X: false otherwise, where X is the case number (starting from 1).

Sample Input:

3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0

Sample Output:

Case #1: false
Case #2: true
Case #3: false

思路:

1.输入三个数,如果A + B > C 输出 'Case #X: true',其他情况下输出'Case #X: false'

2.注意,A,B,C的范围时[-2^64, 2^64],A+B有可能会超出long long的范围。

3.如果超出了正数的范围,那么A+B时<0的,如果超出了负数的范围,那么A+B是>= 0的。

代码:

C/C++:

#include<stdio.h>

int main() {
	int T, t = 1;
	scanf("%d", &T);
	for(int i = 1; i <= T; i++) {
		long long a, b,c;
		scanf("%lld%lld%lld", &a, &b, &c);
		long long res = a + b;
		bool flag;
		if(res < 0 && a > 0 && b > 0) flag = true;
		else if(res >= 0 && a < 0 && b < 0) flag = false;
		else if(res > c) flag = true;
		else flag = false;
		if(flag == true) {
			printf("Case #%d: true\n", t++);
		} else {
			printf("Case #%d: false\n", t++);
		}
	}
	return 0;
} 

Java:

import java.util.Scanner;

/**
 * @author 余修文
 * @date 2018/9/20 9:23
 */
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t;
        long a, b, c, count;
        t = in.nextInt();
        for(int i = 1; i <= t; i++) {
            boolean isFlag;
            a = in.nextLong();
            b = in.nextLong();
            c = in.nextLong();
            count = a + b;
            if(count < 0 && a > 0 && b > 0) {
                isFlag = true;
            } else if(count >= 0 && a < 0 && b < 0) {
                isFlag = false;
            } else if (count > c) {
                isFlag = true;
            } else {
                isFlag = false;
            }
            if(isFlag) {
                System.out.printf("Case #%d: true\n", i);
            } else {
                System.out.printf("Case #%d: false\n", i);
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/Ecloss/article/details/82664603