下一個斐波拉契數列

Write a program that takes input of integer N, followed by N more integers.java

For each integer, output the next fibonacci number after it.git

Fibonacci number: Any number that belongs to the fibonacci series.github

Constraints:數組

Your program should run correctly for the first 69 Fibonacci numbers.測試

Your output lines should not have any trailing or leading whitespace.spa

Input
3
1
9
22


Output
2
13
34

 

Explanation: 2 is the next fibonacci number greater than 1, the fibonacci number that comes after 9 is 13. 34 is the next fibonacci number after 22.debug

英文描述

英文描述請參考下面的圖。code

中文描述

根據給定的值,返回這個值後面的下一個斐波拉契數列中的下一個數。ip

在斐波拉契數列中存儲 60 個 斐波拉契數。ci

例如,給定整數 1,那麼應該返回的結果是 2 。由於給定整數 1 後面的下一個斐波拉契數是 2。

若是給定的數值是 9 的話,那麼下一個斐波拉契數應該是 13。

斐波拉契數列又譯爲菲波拿契數列、菲波那西數列、斐波那契數列、黃金分割數列。

用文字來講,就是費波那契數列由0和1開始,以後的費波那契係數就是由以前的兩數相加而得出。首幾個費波那契係數是:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233……(OEIS中的數列A000045)

思路和點評

首先計算斐波拉契數列,而後將數值存儲到數組中。

定義一個數組,在這個數組中先存儲 60 個從小到大的斐波拉契數。

而後將給定的數值與數值中存儲的斐波拉契數進行對比,這個時候你須要對數組中的斐波拉契數進行遍歷。當找到大於當前給定的整數之後,能夠 Break 此次比對而且返回(輸出)這個值。

源代碼

源代碼和有關代碼的更新請訪問 GitHub:

https://github.com/cwiki-us/codebank-algorithm/blob/master/src/main/java/com/ossez/codebank/interview/ManNextFibonacciNumber.java

運行建議:

這個方法由於測試平臺的問題,咱們沒有寫到測試類中。咱們是直接定義了一個能夠運行的類。

你能夠在你的 Eclipse 平臺上,直接運行這個類。

在你運行類之後的 Console 控制檯窗口,你首先須要輸入數字 3 ,這個數字 3 表示此次運行你須要進行 3 次測試。

而後輸入測試數字,例如,你能夠輸入測試數字 1,那麼,程序將會輸出 1 Next Fibonacci [2]。

這個與實際題目要求的有所差別,你須要進行調整,並且題目是須要使用 System.out.println 輸出的,請注意咱們在咱們的源程序中註釋掉了這個輸出。

代碼思路請參考:

package com.ossez.codebank.interview;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * https://www.cwiki.us/display/ITCLASSIFICATION/Next+Fibonacci+Number
 *
 * @author YuCheng
 *
 */
public class ManNextFibonacciNumber {
    private final static Logger logger = LoggerFactory.getLogger(ManNextFibonacciNumber.class);

    public static void main(String[] args) throws java.lang.Exception {
        int fArray[] = new int[60];

        for (int i = 0; i < 60; i++) {
            fArray[i] = getFib(i);
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input = br.readLine();
        // System.out.println(fib(Integer.valueOf(input)));

        for (int i = 0; i < Integer.valueOf(input); i++) {
            Integer inputInt = Integer.valueOf(br.readLine());
            // System.out.println(inputInt);
            for (int j = 0; j < fArray.length; j++) {
                if (fArray[j] > inputInt) {
                    // System.out.println(fArray[j]);
                    logger.debug("{} Next Fibonacci [{}]", inputInt, fArray[j]);
                    break;
                }
            }

        }

    }

    /**
     * Get Fibonacci Number
     *
     * @param n
     * @return
     */
    private static int getFib(int n) {
        if (n < 0) {
            return -1;
        } else if (n == 0) {
            return 0;
        } else if (n == 1 || n == 2) {
            return 1;
        } else {
            int[] fibAry = new int[n + 1];
            fibAry[0] = 0;
            fibAry[1] = fibAry[2] = 1;
            for (int i = 3; i <= n; i++) {
                fibAry[i] = fibAry[i - 1] + fibAry[i - 2];
            }
            return fibAry[n];
        }
    }
}

 

測試結果

上面程序的測試結果以下:

3
1
2019/02/07 20:59:25 DEBUG [com.ossez.codebank.interview.ManNextFibonacciNumber] - 1 Next Fibonacci [2]
9
2019/02/07 20:59:46 DEBUG [com.ossez.codebank.interview.ManNextFibonacciNumber] - 9 Next Fibonacci [13]
22
2019/02/07 20:59:49 DEBUG [com.ossez.codebank.interview.ManNextFibonacciNumber] - 22 Next Fibonacci [34]
相關文章
相關標籤/搜索