Write a program that takes input of integer N, followed by N more integers.
For each integer, output the next fibonacci number after it.
Fibonacci number: Any number that belongs to the fibonacci series.
Constraints:
Your program should run correctly for the first 69 Fibonacci numbers.
Your output lines should not have any trailing or leading whitespace.
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.
英文描述
英文描述請參考下面的圖已维。
中文描述
根據(jù)給定的值,返回這個值后面的下一個斐波拉契數(shù)列中的下一個數(shù)。
在斐波拉契數(shù)列中存儲 60 個?斐波拉契數(shù)。
例如,給定整數(shù) 1惶洲,那么應(yīng)該返回的結(jié)果是 2 。因為給定整數(shù) 1 后面的下一個斐波拉契數(shù)是 2。
如果給定的數(shù)值是 9 的話赘娄,那么下一個斐波拉契數(shù)應(yīng)該是 13。
斐波拉契數(shù)列又譯為菲波拿契數(shù)列宏蛉、菲波那西數(shù)列擅憔、斐波那契數(shù)列、黃金分割數(shù)列檐晕。
用文字來說暑诸,就是費波那契數(shù)列由0和1開始,之后的費波那契系數(shù)就是由之前的兩數(shù)相加而得出辟灰。首幾個費波那契系數(shù)是:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233……(OEIS中的數(shù)列A000045)
思路和點評
首先計算斐波拉契數(shù)列个榕,然后將數(shù)值存儲到數(shù)組中。
定義一個數(shù)組芥喇,在這個數(shù)組中先存儲 60 個從小到大的斐波拉契數(shù)西采。
然后將給定的數(shù)值與數(shù)值中存儲的斐波拉契數(shù)進(jìn)行對比,這個時候你需要對數(shù)組中的斐波拉契數(shù)進(jìn)行遍歷继控。當(dāng)找到大于當(dāng)前給定的整數(shù)以后械馆,可以 Break 這次比對并且返回(輸出)這個值。
源代碼
源代碼和有關(guān)代碼的更新請訪問 GitHub:
運行建議:
這個方法因為測試平臺的問題武通,我們沒有寫到測試類中霹崎。我們是直接定義了一個可以運行的類。
你可以在你的 Eclipse 平臺上冶忱,直接運行這個類玉锌。
在你運行類以后的 Console 控制臺窗口没佑,你首先需要輸入數(shù)字 3 ,這個數(shù)字 3 表示這次運行你需要進(jìn)行 3 次測試。
然后輸入測試數(shù)字鞋既,例如懈涛,你可以輸入測試數(shù)字 1了讨,那么麦射,程序?qū)敵?1 Next Fibonacci [2]。
這個與實際題目要求的有所差異括勺,你需要進(jìn)行調(diào)整缆八,而且題目是需要使用?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];
????????}
????}
}
測試結(jié)果
上面程序的測試結(jié)果如下:
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]