给定区间[-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
读题目引起思考:数字的区间是[-2的31次方, 2的31次方],正好是int的取值范围的。由此想到可以使用int类型,但两个很大的int类型相加,可能会引起溢出。由此,考虑如下思路,参见代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int T = 0; T = scn.nextInt(); boolean res = false; long a = 0; long b = 0; long c = 0; for (int i = 1; i <= T; i++) { a = scn.nextLong(); b = scn.nextLong(); c = scn.nextLong(); res = judge(a, b, c); if (res) { System.out.println("Case #" + i + ": true"); } else { System.out.println("Case #" + i + ": false"); } } } public static boolean judge(long a, long b, long c) { boolean res = false; if (a <= 0 && b >= 0 || a >= 0 && b <= 0) { // 此时不超过范围,两者可以直接相加减 if (a + b > c) { res = true; } else { res = false; } } else if (a > 0 && b > 0) { // 此时两者相加可能会超过范围,因此需要分开判断 if (a >= c || b >= c) { // 此时,两者的和肯定比c大 res = true; } else { // 这里需要想明白 if (c - a >= b) { // 减法可以避免越界 res = false; } else { res = true; } } } else if (a < 0 && b < 0) { if (a <= c || b <= c) { res = false; } else { if (c - a >= b) { // 减法可以避免越界 res = false; } else { res = true; } } } return res; } }
参考:http://blog.csdn.net/zongyinhu/article/details/46603255
当你把类型改为int时,OJ会报错,,,明明int和long都是四个字节,用long没有错,而改为int却出错????难道int和long有什么区别?
<<c++Primer>>:
short至少16位;
int至少与short一样长;
long至少32位,且至少与int一样长
看一个表:
类型 |
16位系统/字节 |
32位系统/字节 |
64位系统/字节 |
char |
1 |
1 |
1 |
char* |
2 |
4 |
8 |
short |
2 |
2 |
2 |
int |
2 |
4 |
4 |
long |
4 |
4
|
8 |
long long |
8 |
8 |
8
|
int的长度等于字长(cpu处理指令的长度),16位cpu字长是16位,所以int占两个字节,32位cpu字长是32位,所以int占四个字节,64位cpu下由于操作系统只有32位,32位操作系统是针对32位cpu设计的,所以无法发挥一次性处理8个字节的性能,int只能降为4个字节.
如果将类型改为int,由于int在16位cpu下只有两个字节,所以无法表示题目要求的范围,严格要求的OJ当然报错!
由于类型所占字节随环境而异,这可能导致c++程序从一种环境到另外一种环境产生问题,需要注意!!
综上所述:
1:int不一定占4个字节,因环境而异;
2:程序需要考虑移植性问题;
3:long long标准8个字节,不受编译器的限制;
经过上面的一番折腾,最终修改代码使用long类型,这样直接加加减减就AC了。
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int T = 0; long a, b, c; a = b = c = 0; T = scn.nextInt(); for (int i = 1; i <= T; i++) { a = scn.nextLong(); b = scn.nextLong(); c = scn.nextLong(); if (a + b > c) { System.out.println("Case #" + i + ": true"); } else { System.out.println("Case #" + i + ": false"); } } } }