-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubset.java
More file actions
27 lines (26 loc) · 856 Bytes
/
Subset.java
File metadata and controls
27 lines (26 loc) · 856 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
26
27
/*
* Subset client. Write a client program Subset.java that takes a command-line
* integer k, reads in a sequence of N strings from standard input using
* StdIn.readString(), and prints out exactly k of them, uniformly at random.
* Each item from the sequence can be printed out at most once. You may assume
* that k ? 0 and no greater than the number of string on standard input.
*/
public class Subset
{
public static void main(String[] args)
{
RandomizedQueue<String> q = new RandomizedQueue<String>();
int k = Integer.parseInt(args[0]);
while (!StdIn.isEmpty())
{
String item = StdIn.readString();
q.enqueue(item);
}
while (k > 0)
{
StdOut.println(q.dequeue());
k--;
}
return;
}
}