-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem11.java
executable file
·128 lines (99 loc) · 2.8 KB
/
Problem11.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.util.*;
import java.io.File;
import java.io.IOException;
public class helloworld {
public static void main(String []args)
throws IOException {
//System.out.println("Hello World");
Scanner s = new Scanner(new File("ProductGrid.txt"));
System.out.println("Answer: " + Grid(s));
}
public static int Grid(Scanner s) {
int[][] GridArray = new int[20][20];
int row = 0;
int max = 0;
int counter = 0;
int horizProduct = 1;
int vertProduct = 1;
int oneDiagProduct = 1;
int twoDiagProduct = 1;
while (s.hasNextLine()) {
String line = s.nextLine();
String numbers[] = line.split("\\s+");
//System.out.println(numbers.length);
for (int col = 0; col <numbers.length; col++) {
GridArray[row][col] = Integer.parseInt(numbers[col]);
System.out.print(GridArray[row][col] + " ");
}
System.out.println(" ");
row++;
}
System.out.println("Next");
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
//System.out.println (j + " ");
for (int k = j; k < j + 4 && k != 19; k++) {
horizProduct *= GridArray[i][k];
vertProduct *= GridArray[k][i];
counter++;
}
if (counter == 4) {
if (horizProduct > max) {
System.out.println("Horizontal:" + horizProduct + " Values: " + GridArray[i][j]);
max = horizProduct;
}
if (vertProduct > max) {
max = vertProduct;
System.out.println("Vertical:" + vertProduct + " Values: " + GridArray[j][i]);
}
}
counter = 0;
horizProduct = 1;
vertProduct = 1;
//System.out.print(GridArray[i][j] + " ");
}
System.out.println(" ");
}
System.out.println("Next");
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
for (int k = j; k < j + 4 && k < 20; k++) {
if (i + counter > 19) {
break;
}
oneDiagProduct *= GridArray[i + counter][k];
//System.out.print(GridArray[i+counter][k] + " ");
//twoDiagProduct *= GridArray[k][i + counter];
counter++;
}
if (counter == 4) {
if (oneDiagProduct > max) {
max = oneDiagProduct;
System.out.println("Onediag:" + oneDiagProduct + " Values: " + GridArray[j][i]);
}
}
counter = 0;
for (int k = j; k >= j - 3 && k >= 0; k--) {
if (i + counter > 19) {
break;
}
twoDiagProduct *= GridArray[i + counter][k];
//System.out.print(GridArray[i+counter][k] + " ");
//twoDiagProduct++;
counter++;
}
if (counter == 4) {
if (twoDiagProduct > max) {
max = twoDiagProduct;
System.out.println("TwoDiag:" + twoDiagProduct + " Values: " + GridArray[j][i]);
}
}
counter = 0;
oneDiagProduct = 1;
twoDiagProduct = 1;
//System.out.println(" ");
}
}
return max;
}
}