# Next Fibonacci Number（下一个斐波拉契数列）

+关注继续查看

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.

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.

## 中文描述

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233……（OEIS中的数列A000045）

## 源代码

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

package com.ossez.codebank.interview;

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);
}

// System.out.println(fib(Integer.valueOf(input)));

for (int i = 0; i < Integer.valueOf(input); i++) {
// 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]

https://www.cwiki.us/display/ITCLASSIFICATION/Next+Fibonacci+Number

|
8月前
|

LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
LeetCode 1342. 将数字变成 0 的操作次数 Number of Steps to Reduce a Number to Zero
51 0
|
8月前
|

LeetCode 204. Count Primes

19 0
|
8月前
LeetCode 64. Minimum Path Sum

43 0
HDOJ 1003 Max Sum
HDOJ 1003 Max Sum
51 0
HDOJ 2071 Max Num
HDOJ 2071 Max Num
61 0
HDOJ1003Max Sum
HDOJ1003Max Sum
49 0
HDOJ-1003 Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
890 0