-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThrowingCard.java
More file actions
44 lines (40 loc) · 1.11 KB
/
ThrowingCard.java
File metadata and controls
44 lines (40 loc) · 1.11 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
/**
* https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1876
* #queue
*/
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Scanner;
class ThrowingCard {
static void calculate(int n) {
Deque<Integer> queue = new LinkedList<Integer>();
for (int i = 1; i < n + 1; i++) {
queue.offerLast(i);
}
System.out.print("Discarded cards: ");
while (queue.size() > 1) {
System.out.print(queue.pollFirst());
if (queue.size() > 1) {
System.out.print(", ");
}
queue.offerLast(queue.pollFirst());
}
System.out.println("");
String remainingCard = "Remaining card: ";
System.out.println(remainingCard + queue.pollFirst());
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> arr = new ArrayList<>();
while (true) {
int n = sc.nextInt();
if (n == 0) {
break;
}
arr.add(n);
}
sc.close();
arr.forEach(i -> calculate(i));
}
}