题目:给定三条边,请你判断一下能不能组成一个三角形。
输入:输入数据第一行包含一个数M,接下有M行,每行一个实例,包含三个正数A,B,C。其中A,B,C <1000;
输出:对于每个测试实例,如果三条边长A,B,C能组成三角形的话,输出YES,否则NO。
思路:代码1是我想出来的,我觉得脑子问题很大。建议看代码2。只需要3种情况判断一下就可以了。
代码1:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M = in.nextInt();
while (M-- > 0) {
double[] n = new double[3];
for (int i = 0; i < 3; i++) {
n[i] = in.nextDouble();
}
for (int i = 0; i < n.length - 1; i++) {
for (int j = 0; j < n.length - 1; j++) {
if (n[j] > n[j + 1]) {
double temp = n[j];
n[j] = n[j + 1];
n[j + 1] = temp;
}
}
}
if (n[0] + n[1] > n[2]) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}
代码2:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int M = in.nextInt();
while (M-- > 0) {
double A = in.nextDouble();
double B = in.nextDouble();
double C = in.nextDouble();
if (A + B > C && A + C > B && B + C > A){
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
}