Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class mirola {
- public static void main(String[] args) {
- pilha top = new pilha();
- Scanner tec = new Scanner(System.in);
- int jafoi = 0;
- int n = tec.nextInt();
- while(n != 0) {
- if(jafoi == 0) {
- jafoi = 1;
- }else {
- n = tec.nextInt();
- }
- if(n == 1) {
- System.out.println("Discarded cards: ");
- System.out.println("Remaining card: 1");
- }else {
- if(n != 0) {
- for(int i = 0; i < n; i++) {
- top.ins(n - i);
- }
- System.out.printf("Discarded cards:");
- for(int i = 0; i < n; i++) {
- pilha temp1 = top.tirar();
- if(temp1.id != -1) {
- top.botarembaixo(temp1);
- int x = top.tirada;
- System.out.printf(" %d", x);
- }
- if(i < n - 2)
- System.out.print(",");
- }
- System.out.println("");
- System.out.printf("Remaining card: ");
- System.out.println(top.ultima.id);
- }
- }
- }
- }
- }
- class pilha {
- public int id;
- public int tirada;
- public pilha topo;
- public pilha anterior;
- public pilha ultima;
- pilha(){
- this.topo = null;
- this.anterior = null;
- this.tirada = -1;
- this.id = -1;
- this.ultima = null;
- }
- public void ins(int b) {
- pilha a = new pilha();
- a.id = b;
- if(this.topo == null) {
- this.topo = a;
- this.ultima = a;
- }else {
- a.anterior = this.topo;
- this.topo = a;
- }
- }
- public pilha tirar() {
- if(this.topo == null) return new pilha();
- pilha temp = this.topo;
- this.topo = temp.anterior;
- this.tirada = temp.id;
- temp = this.topo;
- this.topo = temp.anterior;
- return temp;
- }
- public int vazio() {
- if(this.topo == null) return 0;
- else return 1;
- }
- public void botarembaixo(pilha a) {
- this.ultima.anterior = a;
- a.anterior = null;
- this.ultima = a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement