fork download
  1. import java.io.PrintStream;
  2. import java.util.Scanner;
  3.  
  4. class Stack {
  5. private static Scanner in = new Scanner(System.in);
  6. class StackElement {
  7. private int data;
  8. private StackElement next;
  9. StackElement(int data, StackElement next) {
  10. this.data = data;
  11. this.next = next;
  12. }
  13. }
  14. private StackElement top = null;
  15. public void push(int chto) {
  16. top = new StackElement(chto, top);
  17. }
  18. public int pop() {
  19. int res = top.data;
  20. top = top.next;
  21. return res;
  22. }
  23. public boolean isEmpty() {
  24. return top == null;
  25. }
  26. public void swap() {
  27. int x = 1001, y = 1001;
  28. if (isEmpty())
  29. System.out.println("Stack is empty!");
  30. else
  31. x = pop();
  32. if (isEmpty())
  33. System.out.println("Stack is empty!");
  34. else
  35. y = pop();
  36. push(x);
  37. push(y);
  38. }
  39. public void dup() {
  40. int x = 1001;
  41. if (isEmpty())
  42. System.out.println("Stack is empty!");
  43. else
  44. x = pop();
  45. push(x);
  46. push(x);
  47. }
  48. public void dup2nd() {
  49. int x = 1001, y = 1001;
  50. if (isEmpty())
  51. System.out.println("Stack is empty!");
  52. else
  53. x = pop();
  54. if (isEmpty())
  55. System.out.println("Stack is empty!");
  56. else
  57. y = pop();
  58. push(y);
  59. push(y);
  60. push(x);
  61. }
  62. public void readPush() {
  63. int x = in.nextInt();
  64. push(x);
  65. }
  66. }
  67.  
  68. public class Main {
  69. public static PrintStream out = System.out;
  70. public static void main(String[] args)
  71. {
  72. Stack a = new Stack();
  73. a.readPush();
  74. a.dup();
  75. a.readPush();
  76. a.push(-a.pop() + a.pop());
  77. a.dup2nd();
  78. a.swap();
  79. a.dup2nd();
  80. a.push(a.pop()-a.pop());
  81. a.swap();
  82. a.readPush();
  83. a.readPush();
  84. a.push(-a.pop() + a.pop() + Math.abs(a.pop()*(a.pop()/a.pop())));
  85. out.print(a.pop());
  86.  
  87. }
  88. }
Success #stdin #stdout 0.1s 35476KB
stdin
10 20 30 40 
stdout
10