Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package stackandqueues;
- import java.math.BigInteger;
- import java.util.ArrayDeque;
- import java.util.ArrayList;
- import java.util.Scanner;
- /**
- *
- * @author kalin
- */
- public class O8_StackAndQueues {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- int nFibonacci = Integer.parseInt(input);
- System.out.println(getFibonacci(nFibonacci, new BigInteger[nFibonacci + 1]));
- }
- private static BigInteger getFibonacci(int nFibonacci,BigInteger[] fibonacciNumber) {
- if (nFibonacci == 0 || nFibonacci == 1) {
- return BigInteger.valueOf(1);
- }
- BigInteger temp = fibonacciNumber[nFibonacci];
- if (temp == BigInteger.ZERO || temp == null) {
- temp = getFibonacci(nFibonacci - 2, fibonacciNumber).add(getFibonacci(nFibonacci - 1, fibonacciNumber));
- fibonacciNumber[nFibonacci] = temp;
- }
- return temp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment