-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA143.java
More file actions
53 lines (44 loc) · 1.75 KB
/
A143.java
File metadata and controls
53 lines (44 loc) · 1.75 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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class A143 {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
Stack<Integer> torre = new Stack<>();
Queue<Integer> colaAux = new LinkedList<>();
//leo por teclado el primer numero.
int numero = sc.nextInt();
//si es igual a 0 se acabo.
while (numero != 0) {
//meto el primer elemento en la pila.
if (numero == -1 && sc.nextInt() == 0) {
break;
}
torre.push(numero);
//Leo el segundo elemento.
numero = sc.nextInt();
//Si es distinto de -1 meto más elementos en la torre hasta que sea -1.
while (numero != -1) {
torre.push(numero);
numero = sc.nextInt();
}
//Leo por teclado el numero de volteos.
int volteos = sc.nextInt();
for (int i = 0; i < volteos; i++) {
//Para cada volteo leo el numero de tortitas que se dan la vuelta.
int nTortitas = sc.nextInt();
//Saco las nTortitas de la Pila y las meto en una cola, para que la de más arriba pase a estar primera de la cola.
for (int j = 0; j < nTortitas; j++) {
colaAux.add(torre.pop());
}
//Vacio la cola metiendo las nTortitas nuevamente en la Pila.
while (!colaAux.isEmpty()) {
torre.push(colaAux.poll());
}
}
System.out.println(torre.peek());
numero = sc.nextInt();
}
}
}