Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dldldld;
- import java.util.*;
- public class dddd {
- public static void main(String[]args) {
- /*Scanner input=new Scanner(System.in);
- Stack<Integer> b1 = new Stack<Integer>();
- Stack<Integer> b2 = new Stack<Integer>();
- int x=0;
- for(int i=0;i<3;i++) {
- System.out.print("b1 "+i+" : ");
- x=input.nextInt();
- b1.push(x);
- System.out.print("b2 "+i+" : ");
- x=input.nextInt();
- b2.push(x);
- }
- Stack<Integer>ans=func(b1,b2);
- while(!(ans.isEmpty())) {
- System.out.print(ans.pop()+" ");
- }
- Stack<Integer> stk1 = buildStack(1);
- Stack<Integer> stk2 = buildStack(2);
- */
- printStack(func());
- }
- public static void printStack(Stack<Integer> s) {
- Stack<Integer> tmp = new Stack<Integer>();
- while(!s.isEmpty()) {
- System.out.println("|_"+s.top()+"_|");
- tmp.push(s.pop());
- }
- while(!tmp.isEmpty()) {
- s.push(tmp.pop());
- }
- }
- /*
- * builds stack for question usage
- * input - required stack by order of the questions
- */
- public static Stack<Integer> buildStack(int stack_num){
- Stack<Integer> stk = new Stack<Integer>();
- switch(stack_num) {
- case 1:
- stk.push(1);
- //stk.push(2);
- //stk.push(4);
- return stk;
- case 2:
- stk.push(2);
- //stk.push(4);
- //stk.push(3);
- return stk;
- default:
- break;
- }
- return stk;
- }
- public static void stam() {
- Stack<Integer> head1=buildStack(1);
- Stack<Integer> tmp = head1;
- Stack<Integer> head2=buildStack(2);
- Stack<Integer> tmp2 = head2;
- System.out.println("tmp1: "+tmp.top());
- System.out.println("tmp2: "+tmp2.top());
- }
- public static Stack<Integer> func(){
- Stack<Integer> st1=buildStack(1);
- Stack<Integer> st2=buildStack(2);
- Stack<Integer> head1=st1;
- Stack<Integer> head2=st2;
- Stack<Integer> ans = new Stack<Integer>();
- /*while(!(st1.isEmpty()) && !(st2.isEmpty())) {
- ans.push(st1.top()+st2.top());
- head1.push(st1.pop());
- head2.push(st2.pop());
- }*/
- for(int i=0;i<3;i++) {
- ans.push(st1.top()+st2.top());
- head1.push(st1.pop());
- head2.push(st2.pop());
- }
- return ans;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement