Advertisement
ramomjcs

Java2

Mar 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class mirola {
  4.  
  5. public static void main(String[] args) {
  6. pilha top = new pilha();
  7. Scanner tec = new Scanner(System.in);
  8. int jafoi = 0;
  9. int n = tec.nextInt();
  10. while(n != 0) {
  11. if(jafoi == 0) {
  12. jafoi = 1;
  13. }else {
  14. n = tec.nextInt();
  15. }
  16. if(n == 1) {
  17. System.out.println("Discarded cards: ");
  18. System.out.println("Remaining card: 1");
  19. }else {
  20. if(n != 0) {
  21. for(int i = 0; i < n; i++) {
  22. top.ins(n - i);
  23. }
  24. System.out.printf("Discarded cards:");
  25. for(int i = 0; i < n; i++) {
  26. pilha temp1 = top.tirar();
  27. if(temp1.id != -1) {
  28. top.botarembaixo(temp1);
  29. int x = top.tirada;
  30. System.out.printf(" %d", x);
  31.  
  32. }
  33. if(i < n - 2)
  34. System.out.print(",");
  35. }
  36. System.out.println("");
  37. System.out.printf("Remaining card: ");
  38. System.out.println(top.ultima.id);
  39.  
  40. }
  41. }
  42. }
  43. }
  44.  
  45. }
  46. class pilha {
  47. public int id;
  48. public int tirada;
  49. public pilha topo;
  50. public pilha anterior;
  51. public pilha ultima;
  52. pilha(){
  53. this.topo = null;
  54. this.anterior = null;
  55. this.tirada = -1;
  56. this.id = -1;
  57. this.ultima = null;
  58. }
  59. public void ins(int b) {
  60. pilha a = new pilha();
  61. a.id = b;
  62. if(this.topo == null) {
  63. this.topo = a;
  64. this.ultima = a;
  65. }else {
  66. a.anterior = this.topo;
  67. this.topo = a;
  68. }
  69. }
  70. public pilha tirar() {
  71. if(this.topo == null) return new pilha();
  72. pilha temp = this.topo;
  73. this.topo = temp.anterior;
  74. this.tirada = temp.id;
  75. temp = this.topo;
  76. this.topo = temp.anterior;
  77. return temp;
  78. }
  79. public int vazio() {
  80. if(this.topo == null) return 0;
  81. else return 1;
  82. }
  83. public void botarembaixo(pilha a) {
  84. this.ultima.anterior = a;
  85. a.anterior = null;
  86. this.ultima = a;
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement