-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSubset.java
More file actions
25 lines (24 loc) · 722 Bytes
/
Copy pathSubset.java
File metadata and controls
25 lines (24 loc) · 722 Bytes
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
public class Subset {
public static void main(String[] args) {
if (args.length != 1) {
throw new IllegalArgumentException();
}
int k = Integer.parseInt(args[0]);
if (k < 0) {
throw new IllegalArgumentException();
}
RandomizedQueue<String> q = new RandomizedQueue<String>();
String line = null;
while (!StdIn.isEmpty()) {
if (!StdIn.hasNextLine()) break;
line = StdIn.readString();
q.enqueue(line);
}
if (k > q.size()) {
throw new IllegalArgumentException();
}
for (int i = 0; i < k; i++) {
StdOut.println(q.dequeue());
}
}
}