SHARE
TWEET

StackFibonacci

a guest Jun 10th, 2016 149 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package StacksAndQueues;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Scanner;
  5. import java.util.Stack;
  6.  
  7. public class StackFibonacci {
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         int n = sc.nextInt();
  12.  
  13.         Stack<BigInteger> fib = new Stack<>();
  14.  
  15.         fib.push(BigInteger.ONE);
  16.         fib.push(BigInteger.ONE);
  17.  
  18.         for (int i = 1; i < n; i++) {
  19.  
  20.             BigInteger tmp = fib.pop();
  21.             BigInteger num = fib.peek().add(tmp);
  22.             fib.push(tmp);
  23.             fib.push(num);
  24.         }
  25.  
  26.         System.out.println(fib.peek());
  27.     }
  28. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top