Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class hanukkah
- {
- static Scanner reader = new Scanner(System.in);
- public static Stack<Integer> a(Stack<Integer> s1, Stack<Integer> s2)
- {
- int temp=0;
- Stack<Integer> h1 = new Stack<Integer>();
- Stack<Integer> h2 = new Stack<Integer>();
- Stack<Integer> sum = new Stack<Integer>();
- while(!s1.isEmpty() && !s2.isEmpty())
- {
- h1.push(s1.pop());
- h2.push(s2.pop());
- }
- while(!h1.isEmpty() && !h2.isEmpty())
- if(h1.top()+h2.top()<10)
- {
- sum.push(h1.top()+h2.top()+temp);
- s1.push(h1.pop()); s2.push(h2.pop());
- }
- else
- {
- temp = temp+sum.pop()/10;
- sum.push((h1.top()+h2.top()+temp)%10);
- s1.push(h1.pop()); s2.push(h2.pop());
- }
- return sum;
- }
- public static void input(Stack<Integer> s)
- {
- System.out.println("Enter a num. !=0");
- int n = reader.nextInt();
- while(n!=0)
- s.push(n);
- }
- public static void main(String[] args)
- {
- Stack<Integer> s1 = new Stack<Integer>();
- Stack<Integer> s2 = new Stack<Integer>();
- Stack<Integer> sum = new Stack<Integer>();
- input(s1);
- input(s2);
- sum=a(s1, s2);
- System.out.println(sum.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement