THINK FIRST·CODE LATER

← All labs

Hot potato: a queue simulation

Problem

Players stand in a circle and pass a potato. In each round the potato is passed k times; whoever holds it then is out. The last player left wins.

Model the circle with a queue (Deque<String> implemented by ArrayDeque): the player at the front holds the potato. One pass moves the front player to the back (offer(poll())). After k passes, the front player is removed.

Input. Two integers n (number of players, n ≥ 0) and k (k ≥ 0), then n player names (one word each), in circle order.

Output. If n is 0 print No players. Otherwise, for every elimination print Round r: name is out (r starting at 1), and finally Winner: name.

Input:
5 2
Ann Ben Cat Dan Eve

Output:
Round 1: Cat is out
Round 2: Ann is out
Round 3: Eve is out
Round 4: Ben is out
Winner: Dan

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d4-2-data-1.txt

5 2
Ann Ben Cat Dan Eve
What your program must print for d4-2-data-1.txt
Round 1: Cat is out
Round 2: Ann is out
Round 3: Eve is out
Round 4: Ben is out
Winner: Dan

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d4-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content d4-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.