A+B and C(java版本)
题目描述:
给出三个整数A,B,C,如果A+B>C,则输出true;否则,输出false。
由于long的范围是[-2^63 ,2^63),因此题目会出现相加可能越界的情况所以定义long类型而不是int类型。
package 算法笔记;
import java.util.Scanner;
//A+B and C(注意本题目中的int型变量容易越界,所以改用Long型)
public class test0004 {
public static void main(String [] args)
{
Scanner sc=new Scanner(System.in);
int n;
n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++)
{
long A,B,C;
A=sc.nextLong();
B=sc.nextLong();
C=sc.nextLong();
if(A+B>C)
a[i]=1;
else a[i]=0;
}
for(int i=0;i<n;i++)
{
if(a[i]==0)
System.out.println("Case#:"+(i+1)+"false");
else
System.out.println("Case#:"+(i+1)+"true");
}
}
}
输入样例:
3
1 2 3
2 3 4
9223372036854775807 -9223372036854775808 0
输出样例:
Case#:1false
Case#:2true
Case#:3false
运行结果截图如下: