-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhwo.txt
More file actions
182 lines (163 loc) · 5.69 KB
/
hwo.txt
File metadata and controls
182 lines (163 loc) · 5.69 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.Arrays;
public class Bingo {
public static void main(String[] args) throws Exception {
// read file from args[0] in Java 7 style
try(BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\user\\Desktop\\Java hw\\hw0\\input.txt"))){
// read a line and split by ','
String[] data = br.readLine().split(",");
// store the first integer in variable stringCount (number of announced strings)
int stringCount = Integer.parseInt(data[0]);
// store the second integer in variable num (dimension of matrix: num * num)
int num = Integer.parseInt(data[1]);
// initilization of a String array in Java
String[] announce = new String[stringCount];
String[][] matrix = new String[num][num];
int [][] bingo = new int[num][num];
// printf in Java (you should comment out or delete this in your final submission)
System.out.printf("number of announced strings: %d\ndimension of matrix: %d x %d\n", stringCount, num, num);
String[] line2 = br.readLine().split(",");
for(int i = 0 ; i < stringCount ; i++)
{
announce[i] = line2[i];
}
for(int a = 0;a < num ;a++)
{
String[] line = br.readLine().split(",");
for(int b = 0; b < num ; b++)
{
matrix[a][b] = line[b];
bingo[a][b] = 1;
}
}
for(int i = 0; i < stringCount;i++)
{
for(int j =0; j < num;j++)
{
for(int k = 0; k < num;k++)
{
if(announce[i].compareTo(matrix[j][k])==0)
{
bingo[j][k] = -1;
}
}
}
}
int count = 0;
int win = 0;
for(int a =0;a<num;a++)
{
for(int b =0; b<num;b++)
{
if(bingo[a][b] == -1)
{
count++;
}
else
{
count = count;
}
}
if(count == num)
{
win++;
}
else
{
count = 0;
}
}
for(int c = 0; c< num;c++)
{
for(int r = 0; r < num;r++)
{
if(bingo[r][c] == -1)
{
count++;
}
else
{
count = count;
}
}
if(count == num)
{
win++;
}
else
{
count = 0;
}
}
count = 0;
for(int a =0;a<num;a++)
{
for(int b = 0; b < num;b++)
{
if(a == b && bingo[a][b] == -1)
{
count++;
}
else
{
count = count;
}
}
if(count == num)
{
win++;
}
else
{
count = 0;
}
}
count = 0;
for(int a =0;a<num;a++)
{
for(int b = 0; b < num;b++)
{
if((a+b) == num)
{
if(bingo[a][b] == -1)
{
count++;
}
else
{
count = count;
}
}
}
if(count == num)
{
win++;
}
else
{
count = 0;
}
}
/* now you can write your own solution to hw0
* you can follow the instruction described below:
*
* 1. read the rest content of the file
* 2. store the announce strings (2nd line of the file) in variable announce
* 3. store the matrix (from the 3rd line to the end of the file) in variable matrix
* 4. compare the matrix and announce strings (this is the tricky part)
* 5. output how many 'straight line' are there in the matrix
*
* [note]
* you can use every data structure in standard Java packages (Java 8 supported)
* the packages in stdlib.jar and algs4.jar are also available for you to use
*
* [hint]
* 1. you should check whether Java pass the variable by references or by values.
* 2. some data structure such as HashSet, HashMap, Arrays, ArrayList, Vector are very
* useful for solving problems.
*/
System.out.println("total line is"+" "+win);
}
}
}