-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlowLayout.java
45 lines (34 loc) · 1.25 KB
/
FlowLayout.java
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
42
43
44
45
import java.util.Scanner;
public class FlowLayout {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int maxWidth = scanner.nextInt();
while(maxWidth != 0) {
int windowWidth = 0;
int windowHeight = 0;
int rowWidth = 0;
int rowHeight = 0;
int givenWidth = scanner.nextInt();
int givenHeight = scanner.nextInt();
while (givenWidth != -1) {
if (rowWidth + givenWidth <= maxWidth) {
rowWidth += givenWidth;
rowHeight = Math.max(rowHeight, givenHeight);
}
else {
windowWidth = Math.max(windowWidth, rowWidth);
windowHeight += rowHeight;
rowWidth = givenWidth;
rowHeight = givenHeight;
}
givenWidth = scanner.nextInt();
givenHeight = scanner.nextInt();
}
windowWidth = Math.max(windowWidth, rowWidth);
windowHeight += rowHeight;
System.out.println(windowWidth + " x " + windowHeight);
maxWidth = scanner.nextInt();
}
scanner.close();
}
}