Guest User

Untitled

a guest
Dec 8th, 2019
136
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package dldldld;
  2. import java.util.*;
  3. public class dddd {
  4. public static void main(String[]args) {
  5. /*Scanner input=new Scanner(System.in);
  6. Stack<Integer> b1 = new Stack<Integer>();
  7. Stack<Integer> b2 = new Stack<Integer>();
  8. int x=0;
  9. for(int i=0;i<3;i++) {
  10. System.out.print("b1 "+i+" : ");
  11. x=input.nextInt();
  12. b1.push(x);
  13. System.out.print("b2 "+i+" : ");
  14. x=input.nextInt();
  15. b2.push(x);
  16. }
  17. Stack<Integer>ans=func(b1,b2);
  18. while(!(ans.isEmpty())) {
  19. System.out.print(ans.pop()+" ");
  20. }
  21.  
  22. Stack<Integer> stk1 = buildStack(1);
  23. Stack<Integer> stk2 = buildStack(2);
  24. */
  25. printStack(func());
  26.  
  27.  
  28. }
  29. public static void printStack(Stack<Integer> s) {
  30. Stack<Integer> tmp = new Stack<Integer>();
  31. while(!s.isEmpty()) {
  32. System.out.println("|_"+s.top()+"_|");
  33.  
  34. tmp.push(s.pop());
  35. }
  36. while(!tmp.isEmpty()) {
  37.  
  38. s.push(tmp.pop());
  39. }
  40. }
  41. /*
  42. * builds stack for question usage
  43. * input - required stack by order of the questions
  44. */
  45. public static Stack<Integer> buildStack(int stack_num){
  46.  
  47. Stack<Integer> stk = new Stack<Integer>();
  48. switch(stack_num) {
  49. case 1:
  50.  
  51. stk.push(1);
  52. //stk.push(2);
  53. //stk.push(4);
  54. return stk;
  55.  
  56. case 2:
  57. stk.push(2);
  58. //stk.push(4);
  59. //stk.push(3);
  60. return stk;
  61. default:
  62. break;
  63.  
  64. }
  65. return stk;
  66. }
  67. public static void stam() {
  68. Stack<Integer> head1=buildStack(1);
  69. Stack<Integer> tmp = head1;
  70.  
  71. Stack<Integer> head2=buildStack(2);
  72. Stack<Integer> tmp2 = head2;
  73.  
  74. System.out.println("tmp1: "+tmp.top());
  75. System.out.println("tmp2: "+tmp2.top());
  76. }
  77.  
  78. public static Stack<Integer> func(){
  79. Stack<Integer> st1=buildStack(1);
  80. Stack<Integer> st2=buildStack(2);
  81. Stack<Integer> head1=st1;
  82. Stack<Integer> head2=st2;
  83. Stack<Integer> ans = new Stack<Integer>();
  84. /*while(!(st1.isEmpty()) && !(st2.isEmpty())) {
  85. ans.push(st1.top()+st2.top());
  86. head1.push(st1.pop());
  87. head2.push(st2.pop());
  88. }*/
  89. for(int i=0;i<3;i++) {
  90. ans.push(st1.top()+st2.top());
  91. head1.push(st1.pop());
  92. head2.push(st2.pop());
  93. }
  94. return ans;
  95. }
  96.  
  97. }
RAW Paste Data