fork(1) download
  1. //e-olymp 6122. Простой стек
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class Stack {
  8.  
  9. Vector<Integer> storage;
  10.  
  11. public Stack()
  12. {
  13. storage = new Vector<Integer>();
  14. }
  15.  
  16. void push(int number)
  17. {
  18. this.storage.add(number);
  19. }
  20.  
  21. int pop()
  22. {
  23. int lastNumber = storage.lastElement();
  24. storage.remove(storage.size() - 1);
  25. return lastNumber;
  26. }
  27.  
  28. int back()
  29. {
  30. int lastNumber = storage.lastElement();
  31. return lastNumber;
  32. }
  33.  
  34. int size()
  35. {
  36. return storage.size();
  37. }
  38.  
  39. void clear()
  40. {
  41. storage.clear();
  42. }
  43.  
  44. String exit()
  45. {
  46. return "bye";
  47. }
  48.  
  49. }
  50.  
  51.  
  52. public class Main {
  53.  
  54. public static void main(String[] args) {
  55. Stack stack = new Stack();
  56. Scanner input = new Scanner(System.in);
  57. int number;
  58. String request;
  59. while(input.hasNextLine())
  60. {
  61. request = input.next();
  62. if(request.equals("push"))
  63. {
  64. number = input.nextInt();
  65. stack.push(number);
  66. System.out.println("ok");
  67. }
  68. else if(request.equals("pop")) System.out.println(stack.pop());
  69. else if(request.equals("back")) System.out.println(stack.back());
  70. else if(request.equals("size")) System.out.println(stack.size());
  71. else if(request.equals("clear"))
  72. {
  73. stack.clear();
  74. System.out.println("ok");
  75. }
  76. else if(request.equals("exit"))
  77. {
  78. System.out.println(stack.exit());
  79. break;
  80. }
  81. }
  82. }
  83. }
  84.  
Success #stdin #stdout 0.06s 4386816KB
stdin
push 2
push 3
push 5
back
size
pop
size 
push 7 
pop 
clear 
size 
exit
stdout
ok
ok
ok
5
3
5
2
ok
7
ok
0
bye