Boxes
# Topic
Iterating over arrays
# Problem
Boxes
There are two boxes on the table. The first box has a size of X1 x Y1 x Z1, and the second box has a size of X2 x Y2 x Z2. You need to determine whether one of the boxes can be put inside the other. It should go in without sticking out. You can rotate both boxes as you want.
Important: two equally sized boxes cannot be placed inside one another. See the third test case as an example of how the borderline case should be treated.
Input consists of two lines:
- the first line contains numbers X1, Y1, Z1;
- the second line contains numbers X2, Y2, Z2.
All numbers are integers and greater than 0.
Output:
- "Box 1 < Box 2", if the first box can be put inside the second box ;
- "Box 1 > Box 2", if the second box can be put inside the first box;
- otherwise, output "Incompatible".
Sample Input 1:
1 2 3
5 6 4
1
2
2
Sample Output 1:
Box 1 < Box 2
1
Sample Input 2:
2 9 4
3 8 1
1
2
2
Sample Output 2:
Box 1 > Box 2
1
Sample Input 3:
1 3 7
2 8 3
1
2
2
Sample Output 3:
Incompatible
1
# Solutions
# Other solution 1
import java.util.Scanner;
import java.util.Arrays;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] box1 = new int[3];
int[] box2 = new int[3];
int countA = 0;
int countB = 0;
for (int i = 0; i < 3; i++) {
box1[i] = scanner.nextInt();
}
for (int i = 0; i < 3; i++) {
box2[i] = scanner.nextInt();
}
Arrays.sort(box1);
Arrays.sort(box2);
for (int i = 0; i < 3; i++) {
if (box1[i] < box2[i]) {
countA++;
} else if (box1[i] > box2[i]) {
countB++;
}
}
if (countA == 3) {
System.out.println("Box 1 < Box 2");
} else if (countB == 3) {
System.out.println("Box 1 > Box 2");
} else {
System.out.println("Incompatible");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# Other solution 2
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> box1 = new ArrayList<>();
box1.add(scanner.nextInt());
box1.add(scanner.nextInt());
box1.add(scanner.nextInt());
List<Integer> box2 = new ArrayList<>();
box2.add(scanner.nextInt());
box2.add(scanner.nextInt());
box2.add(scanner.nextInt());
Collections.sort(box1);
Collections.sort(box2);
boolean box1Huge = box1.get(0) > box2.get(0) && box1.get(1) > box2.get(1) && box1.get(2) > box2.get(2);
boolean box2Huge = box1.get(0) < box2.get(0) && box1.get(1) < box2.get(1) && box1.get(2) < box2.get(2);
System.out.println(box1Huge ? "Box 1 > Box 2" :
box2Huge ? "Box 1 < Box 2" : "Incompatible");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
编辑 (opens new window)
上次更新: 2022/09/25, 10:41:23