[英]Iterating over arrays Boxes
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Box 1 dimensions
int x1 = scanner.nextInt();
int y1 = scanner.nextInt();
int z1 = scanner.nextInt();
int[] box1 = {x1, y1, z1};
Arrays.sort(box1);
x1 = box1[0];
y1 = box1[1];
z1 = box1[2];
// Box 2 dimensions
int x2 = scanner.nextInt();
int y2 = scanner.nextInt();
int z2 = scanner.nextInt();
int[] box2 = {x2, y2, z2};
Arrays.sort(box2);
x2 = box2[0];
y2 = box2[1];
z2 = box2[2];
if (x1 < x2) {
if (y1 <= y2) {
if (z1 <= z2) {
System.out.println("Box 1 < Box 2");
}
else {
System.out.println("Incompatible");
}
}
else {
System.out.println("Incompatible");
}
}
else if (x1 > x2) {
if (y1 >= y2) {
if (z1 >= z2) {
System.out.println("Box 1 > Box 2");
}
else {
System.out.println("Incompatible");
}
}
else {
System.out.println("Incompatible");
}
}
else if (x1 == x2) {
if (y1 < y2) {
if (z1 <= z2) {
System.out.println("Box 1 < Box 2");
}
else {
System.out.println("Incompatible");
}
}
else if (y1 > y2) {
if (z1 >= z2) {
System.out.println("Box 1 > Box 2");
}
else {
System.out.println("Incompatible");
}
}
else {
if (z1 < z2) {
System.out.println("Box 1 < Box 2");
}
else if (z1 > z2) {
System.out.println("Box 1 > Box 2");
}
else {
System.out.println("Box 1 = Box 2");
}
}
}
}
}
你能幫我解決你的代碼嗎? 我認為有一些錯誤,因為:
第 3 題(共 7 題)未通過。答案錯誤
這是問題陳述中的示例測試!
測試輸入:
1 3 7
2 8 3
正確的 output:
Incompatible
您的代碼 output:
Box 1 < Box 2
我認為從制定您要解決的要求開始總是好的。 因此,您要確定兩個尺寸為 x、y、z 的框,如果有的話,哪個框可以放入另一個框。 我假設具有相同尺寸的盒子不會彼此適合。
通過按升序對維度進行排序,您將首先比較兩個最小的維度,然后是第二小的維度,依此類推。
所有這些排序尺寸都小於另一個的盒子將適合另一個盒子:
box A: 1, 4, 8
box B: 2, 5, 9
1<2, 4<5, 8<9 -> A < B
如果排序后的維度具有矛盾的排名,則框不兼容:
box A: 1, 3, 7
box B: 2, 8, 3
1<2, 3<8, !7<9 -> Incompatible
所以我認為你的代碼可以更簡單:
if (x1 < y1 && x2 < y2 && x3 < y3) {
System.out.println("Box 1 < Box 2");
}
else if (x1 > y1 && x2 > y2 && x3 > y3) {
System.out.println("Box 1 > Box 2");
}
else {
System.out.println("Incompatible");
}
是的,它的工作,謝謝謝謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.