java - 检查 a,b,c 边的直角是否有效。但是您不能使用循环、条件、数组或更高级的功能

标签 java

我有整数 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))
);

最佳答案

最小边是abc中的最小值;最大边是abc中的最大值;另一边是abc之和减去最小边和最大边。然后,我们需要做的就是检查最小边是否大于 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/

相关文章:

jsp - 将 Java Restful Service 中的值填充到 JSP 页面中

java - 为什么这个 Lucene 查询没有返回任何匹配项?

java - gradle 5中的sourcesJar任务

java - 无法从一个 JSP 移动到另一个 JSP

java - Java 可以在没有 Java 虚拟机的情况下运行吗?

java - 如何使用协调器布局将 View 折叠到屏幕底部?

java - 动画时间延迟

java - 将 Java 对象序列化为 Java 代码

java - 为什么 Java 中没有最终接口(interface)?

java - 如何使用 try 和资源将 while 循环放入线程中?