我有整数 a、b 和 c。
有效的直角是指所有的边都是正整数,它们构成一个有效的直角三角形。
然后我必须输出结果(简单)。
完整免责声明:This is the course and assignment that I'm trying to finish
我的尝试(Java):
// int a, b, c = 3, 4, 5;
// how do I even start checking if I'm not allowed to use "if / else"
// therefore not shown in code
int aSquare = a * a;
int bSquare = b * b;
int cSquare = c * c;
// *Im hoping they dont flag this as a conditional
System.out.println(
(aSquare == (bSquare + cSquare) || bSquare == (cSquare + aSquare)
|| cSquare == (aSquare + bSquare))
);
最佳答案
最小边是a
、b
、c
中的最小值;最大边是a
、b
和c
中的最大值;另一边是a
、b
、c
之和减去最小边和最大边。然后,我们需要做的就是检查最小边是否大于 0,并且最小边的平方加上中间边的平方等于最大边的平方。
final int smallest = Math.min(a, Math.min(b, c));
final int largest = Math.max(a, Math.max(b, c));
final int middle = a + b + c - smallest - largest;
System.out.println(smallest > 0 && smallest * smallest + middle * middle == largest * largest);
关于java - 检查 a,b,c 边的直角是否有效。但是您不能使用循环、条件、数组或更高级的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61758439/