class Test{
static int value=1;
static int f (int n ){
int temp=0;
if(n==0||n==1)
return n;
else{
System.out.println("n="+n);
temp=f(n-1)+f(n-2);
if(temp>value){
System.out.println(value+" ");
value=temp;
}
System.out.println("temp= "+temp);
return temp;
}
}
public static void main(String[] a){
System.out.println("0 1");
f(7);
}
}
static int value=1;
static int f (int n ){
int temp=0;
if(n==0||n==1)
return n;
else{
System.out.println("n="+n);
temp=f(n-1)+f(n-2);
if(temp>value){
System.out.println(value+" ");
value=temp;
}
System.out.println("temp= "+temp);
return temp;
}
}
public static void main(String[] a){
System.out.println("0 1");
f(7);
}
}
temp = f(n - 1) + f(n - 2);
一个数等于前2个数的和
f(n)=f(n - 1) + f(n - 2);
f(n - 1)=f(n - 2)+f(n - 3)
f(n - 2)=f(n - 3);+f(n - 4);直到n=1
1,1,2,3,5,8,13,21
final static int value=50;
public static int getF(){
int lo=0,hi=1;
while(hi<value){
hi=lo+hi;
lo=hi-lo;
System.out.println(lo);
}
return lo;
} public static void main(String[] a){
Test.getF();
}
}
这个程序显示的才是真的febbonacci哪个不知道是什么书列,好乱啊
0 1
n=7
n=6
n=5
n=4
n=3
n=2
temp= 1
1
temp= 2
n=2
temp= 1
2
temp= 3
n=3
n=2
temp= 1
temp= 2
3
temp= 5
n=4
n=3
n=2
temp= 1
temp= 2
n=2
temp= 1
temp= 3
5
temp= 8
n=5
n=4
n=3
n=2
temp= 1
temp= 2
n=2
temp= 1
temp= 3
n=3
n=2
temp= 1
temp= 2
temp= 5
8
temp= 13
System.out.println("n="+n);
temp=f(n-1)+f(n-2);
这个不就是递归吗?就是前2个数的和if(n==0||n==1)return n;前2项在这写的