public class Fibonacci {
public static long fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}
}
假设用户输入“java Fibonacci 7”,结果如下: 1:1
2:1
3:2
4:3
5:5
6:8
7:13
我似乎完全不知道这是如何运作的。从数字3开始。当fib(i)方法通过3时,它不应该返回3,因为如果n = 3那么fib(n-1)/ 的总和是2 /和fib(n-2)/ 其中1 /是3.等等其他数字。
答案 0 :(得分:17)
这是一个更简单的代码,用于生成斐波那契序列,如'0 1 1 2 3 ......'。
public static void main (String[] args) {
int f = 0;
int g = 1;
for (int i = 1; i <= 10; i++) {
System.out.print(f + " ");
f = f + g;
g = f - g;
}
System.out.println();
}
答案 1 :(得分:3)
如果您将3传递给您的函数,它将执行以下操作:
fib(3) = fib(2) + fib(1) //so we we are into the else statement, because 3 > 1
= fib(2) + 1 //fib(1) = 1 because 1 <= 1 so you return it (if statement)
= (fib(1) + fib(0)) + 1 //2 > 1 => we go to the else statement
= (1 + 0) + 1 //0 <= 1 & 1 <= 1 so we are into the if and return the values
= 2
答案 2 :(得分:2)
F(n)
/ \
F(n-1) F(n-2)
/ \ / \
F(n-2) F(n-3) F(n-3) F(n-4)
/ \
F(n-3) F(n-4)
请注意,许多计算都会重复! 需要注意的重要一点是该算法是指数的,因为它不存储先前计算的数字的结果。例如,F(n-3)被称为3次。
有关详细信息,请参阅dasgupta第0.2章的算法
答案 3 :(得分:2)
public static int f(int n){
if (n <= 1) {
return n;
} else {
return f(n - 1) + f(n - 2);
}
}
public static void main(String[] args){
Integer c = 4;
Integer answer = f(c);
System.out.println("Fibonacci " + c + "'s number is: " + answer);
}
答案 4 :(得分:2)
你可以分两行完成!
执行此操作的最短输入(尽管不是最有效的):
int fib( int x ) {
return x > 1 ? fib(x - 1) + fib(x - 2) : x; }
如果你想要一个快速的算法,那就试试这个:
///fast version fibbonacci sequence.
public static float fibonacci(int x){
float[] sequence = new float[x];
sequence[0] = 1;
sequence[1] = 1;
if (x > 1){
for (int i = 2; i < x; i++){
sequence[i] = sequence[i-1] + sequence[i-2];
}
}
for (float z : sequence){
System.out.print("{ " + z + "}, ");
}
return sequence[x-1];
}
答案 5 :(得分:2)
我使用Java 8 Stream的解决方案:
public class Main {
public static void main(String[] args) {
final int n = 7;
Fibonacci fibonacci = new Fibonacci();
Stream.generate(fibonacci::next)
.limit(n)
.forEach(System.out::println);
}
}
public class Fibonacci {
private long next = 1;
private long current = 1;
private int count = 1;
public FibonacciNumber next() {
FibonacciNumber fibonacciNumber = new FibonacciNumber(count++, current);
long previous = current;
current = next;
next = current + previous;
return fibonacciNumber;
}
}
public class FibonacciNumber {
private final int count;
private final long value;
public FibonacciNumber(int count, long value) {
this.count = count;
this.value = value;
}
@Override
public String toString() {
return count + ": " + value;
}
}
答案 6 :(得分:1)
为什么不尝试这个简单的代码。 它与我们在没有递归的Fibonacci中一样。
public class FinnonnacciDemo2 {
static int no = 0, n = 8;
public static void main(String[] args) {
// This will print series till 8
fib(0, 1);
}
public static void fib(int a, int b) {
// Terminating condition.
if (a >= n) {
return;
}
else {
System.out.print("\t" + no);
no = a + b;
a = b;
b = no;
fib(a, b);
}
}
}
答案 7 :(得分:0)
1、1、2、3、5、8、13、21、34、55、89
public void fibonacci(){
int n1 = 0;
int n2 = 1;
int n3;
int count = 10;
Log.d("DBG",n1 + " , " + n2);
for (int i = 1; i <= count ; i++) {
n3 = n1 + n2;
Log.d("DBG",i + "= " + n3+"");
n1 = n2;
n2 = n3;
}
}
答案 8 :(得分:0)
我认为你将Fibonacci序列中的索引(n)与该索引处的实际值混淆。 fib(n = 3)= fib(n-1 = 2)+ fib(n-2 = 1)= 1 + 1 = 2
答案 9 :(得分:0)
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
Scanner scn = new Scanner(System. in );
System.out.println("Enter a number upto which fibonacci numbers should be displayed");
int fib = scn.nextInt();
int a = 0,
b = 1,
res = 0;
System.out.print(a + " " + b + " ");
while (res <= fib) {
res = a + b;
a = b;
b = res;
if (res > fib) break;
System.out.print(res + " ");
}
}
}
答案 10 :(得分:0)
//Big O notation O(1)
private void febonacciSeries(int length) {
ArrayList<Long> fobannachiSeries = new ArrayList<Long>();
System.out.println("Print Fabonacci Series : ");
for(int i=0; i<length;i++){
if(i==0){
fobannachiSeries.add(0L);
}else if(i==1){
fobannachiSeries.add(1L);
}else{
fobannachiSeries.add(fobannachiSeries.get(i-1) + fobannachiSeries.get(i-2));
}
System.out.print(fobannachiSeries.get(i) + " ");
}
}
答案 11 :(得分:0)
这是我使用BigInteger的动态编程实现。
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* Topic: Dynamic Programming
* Algorithm: Fib Algorithm Implementation
*
* Author: Chris M. Perez Santiago
* Date: 4 / 15 / 2018
**/
public class Main {
private static BigInteger[] b = new BigInteger[100000001];
private static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
long n = console.nextLong();
for(long i=1;i<=n;i++) System.out.println(initFib(i));
}
private static BigInteger dpFib(Map<Long , BigInteger> map , long n){
if(map.containsKey(n)) return map.get(n);
map.put(n - 1 , dpFib(map , n - 1));
map.put(n - 2 , dpFib(map , n - 2));
BigInteger sum = map.get(n - 1).add(map.get(n - 2));
map.put(n , sum);
return sum;
}
private static BigInteger initFib(long n){
if(BigInteger.valueOf(n).equals(BigInteger.ONE) || BigInteger.valueOf(n).equals(BigInteger.ZERO))
return BigInteger.ONE;
Map<Long , BigInteger> map = new HashMap<>();
map.put(1L , BigInteger.ONE);
map.put(2L , BigInteger.ONE);
return dpFib(map , n);
}
}
答案 12 :(得分:0)
为什么不使用下一个公式:
private static int fibonacci(int n) {
if (n == 1) {
return 1;
} else if (n == 2) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
答案 13 :(得分:0)
以下方法返回索引n处的斐波纳契数。
public static long getFibonacci(int n) {
List<Long> fibonacciNumbers = new ArrayList();
long j = 0;
int k = 0;
for (int i = 0; i <= n; i++) {
System.out.println("i = " + i);
if (i == 2) {
j += (i - 2);
} else if (i > 2) {
j = fibonacciNumbers.get(i - 1) + fibonacciNumbers.get(k - 2);
} else {
j += i;
}
fibonacciNumbers.add(j);
k++;
System.out.println("Fibonacci Array = " + Arrays.toString(fibonacciNumbers.toArray()));
}
return fibonacciNumbers.get(fibonacciNumbers.size() - 1);
}
答案 14 :(得分:0)
import java.math.BigDecimal;
public abstract class Fibonacci {
public static void main(String[] args) {
Integer max = Integer.valueOf(args[0]);
BigDecimal[] fibs = { new BigDecimal(0), new BigDecimal(1) };
BigDecimal current;
System.out.print("1 ");
for (int i = 0; i < max + 1; i++) {
current = fibs[1].add(fibs[0]);
System.out.print(current + " ");
fibs[0] = fibs[1];
fibs[1] = current;
}
}
}
答案 15 :(得分:0)
Fibonacci数是先前连续两个数的计算总和。它从0 1 1 2 3 5 8开始......依此类推。它就像 -
fib(3) = fib(2) + fib(1)
= fib(1) + fib(0) + 1 //fib(1) = 1
= 1 + 0 + 1 //fib(0) = 0
= 2
递归方法效率较低,因为它涉及使用堆栈的函数调用,如果频繁调用函数来计算较大的斐波纳契数,也有可能出现堆栈溢出。 < / p>
答案 16 :(得分:0)
是一种递归方法...... fib(3)=fib(3-1)+fib(3-2)=
fib(2)+fib(1)=(fib(2-1)+fib(1-1))+fib(1)=
fib(1)+fib(0)+fib(1)=
1 + 0 + 1
答案 17 :(得分:0)
Fibonacci系列从零开始或以1开头,其中没有选项将3作为第三个数字。
1 1 2 3 5 ...
0 1 1 2 3 5 8 ...
通常的方法是使用第二个选项,但从索引0开始,以便fib(0)= 0,fib(1)= fib(2)= 1,如下所示:http://oeis.org/A000045 < / p>
答案 18 :(得分:-2)
import java.util.Scanner;
public class Fibonacci2 {
public static void main(String[]args){
int a;
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Number of Fibonacci numbers to print: ");
a = sc.nextInt();
sc.close();
}
int c=1; /*c current number b last number*/
int b=0;
System.out.println(b);
System.out.println(c);
int bb;
for (int z = 2; z < a ; z++){
bb = b;
b = c;
c = bb + b; /*bb last last number*/
System.out.println(z);
}
}
}