Advertisement
Guest User

Untitled

a guest
Dec 9th, 2012
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.*;
  2. public class hanukkah
  3. {
  4.     static Scanner reader = new Scanner(System.in);
  5.    
  6.     public static Stack<Integer> a(Stack<Integer> s1, Stack<Integer> s2)
  7.     {
  8.         int temp=0;
  9.         Stack<Integer> h1 = new Stack<Integer>();
  10.         Stack<Integer> h2 = new Stack<Integer>();
  11.         Stack<Integer> sum = new Stack<Integer>();
  12.        
  13.         while(!s1.isEmpty() && !s2.isEmpty())
  14.         {
  15.             h1.push(s1.pop());
  16.             h2.push(s2.pop());
  17.         }
  18.        
  19.         while(!h1.isEmpty() && !h2.isEmpty())
  20.             if(h1.top()+h2.top()<10)
  21.                 {
  22.                 sum.push(h1.top()+h2.top()+temp);
  23.                 s1.push(h1.pop()); s2.push(h2.pop());
  24.                 }
  25.             else
  26.             {
  27.                 temp = temp+sum.pop()/10;
  28.                 sum.push((h1.top()+h2.top()+temp)%10);
  29.                 s1.push(h1.pop()); s2.push(h2.pop());
  30.             }
  31.        
  32.        
  33.         return sum;
  34.     }
  35.    
  36.     public static void input(Stack<Integer> s)
  37.     {
  38.        
  39.         System.out.println("Enter a num. !=0");
  40.         int n = reader.nextInt();
  41.         while(n!=0)
  42.         s.push(n);
  43.     }
  44.    
  45.     public static void main(String[] args)
  46.     {
  47.         Stack<Integer> s1 = new Stack<Integer>();
  48.         Stack<Integer> s2 = new Stack<Integer>();
  49.         Stack<Integer> sum = new Stack<Integer>();
  50.         input(s1);
  51.         input(s2);
  52.        
  53.         sum=a(s1, s2);
  54.         System.out.println(sum.toString());
  55.     }
  56.    
  57.  
  58.    
  59.    
  60.    
  61.    
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement