剑指offer

A+B和C

2019-06-20  本文已影响0人  G_uest

题目来源:牛客网--A+B和C

题目描述

给定区间[-2的31次方, 2的31次方]内的3个整数A、B和C,请判断A+B是否大于C。

输入描述

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

输出描述

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从1开始)。

输入例子

4
1 2 3
2 3 4
2147483647 0 2147483646
0 -2147483648 -2147483647

输出例子

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

java代码

import java.util.Scanner;

public class ABC {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        boolean[] res = new boolean[n];
        //计算
        for(int i=0;i<n;i++){
            Long a = in.nextLong();
            Long b = in.nextLong();
            Long c = in.nextLong();
            
            if(a+b>c){
                res[i] = true;
            }else{
                res[i] = false;
            }
        }
        //输出
        for(int i=0;i<n;i++){
            if(res[i]){
                System.out.println("Case #"+(i+1)+": true");            
            }else{
                System.out.println("Case #"+(i+1)+": false");
            }
        }
    }
}

python代码

def ABC():
    n = int(input())
    res = []
    for i in range(n):
        a,b,c = map(int,input().split())
        if(a+b > c):
            res.append(True)
        else:
            res.append(False)
    for inx,val in enumerate(res):
        if(val):
            print("Case #" + str(inx+1) +": true")
        else:
            print("Case #" + str(inx+1) + ": false")

ABC()


补充

属性 最大值 最小值 备注 大小
short 32768 -32768 2的15次方 2byte
int 2147483648 -2147483648 2的31次方 4byte
long 9223372036854775807 -9223372036854775807 2的63次方 8byte
上一篇 下一篇

猜你喜欢

热点阅读