package agrisom;
/*
问题:
已知k阶裴波那契序列的定义为
f0=0, f1=0, ..., fk-2=0, fk-1=1;
fn=fn-1+fn-2+...+fn-k, n=k,k+1,...
举例说明:
(1)k=2时,即2阶裴波那契序列定义为:
f0=0, f1=1;
f2=f1+f0,
f3=f2+f1,
f4=f3+f2,
.....
(2)k=3时,即3阶裴波那契序列定义为:
f0=0, f1=0,f2=1;
f3=f2+f1+f0,
f4=f3+f2+f1,
f5=f4+f3+f2,
.....
*/
/**
* 动态规划求k阶裴波那契序列
*/
public class Fibonacci {
private int factor; //裴波那契序列阶数
private int length; //数列长度
private int [] fibo; //数列数组
/**
* length:数组大小
* factor:阶数(阶数大于1)
*/
public Fibonacci(int length,int factor){
if(factor<2){
System.out.println("阶数必须大于1");
factor=2;
}
this.factor=factor;
this.length=length;
this.fibo=new int[length];
for(int i=0;i<factor-1;i++){
this.fibo[i]=0;
}
this.fibo[factor-1]=1;
this.print();
}
/**
* pos:数列项数
*/
public int dp(int pos){
for(int i=this.factor;i<this.length;i++){
for(int j=i-1;j>=i-this.factor;j--){
this.fibo[i]+=this.fibo[j];
}
this.print();
if(i==pos-1){
return this.fibo[pos-1];
}
}
return 0;
}
/**
* 打印数组
*/
public void print(){
for(int i=0;i<this.fibo.length;i++){
System.out.print(this.fibo[i]+" ");
}
System.out.println();
}
public static void main(String [] args){
Fibonacci fino=new Fibonacci(20,4);
int value=fino.dp(15);
System.out.println(value);
}
}
/**
运行结果:
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 208 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 208 401 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 208 401 773 0 0 0 0 0
773
*/
/*
问题:
已知k阶裴波那契序列的定义为
f0=0, f1=0, ..., fk-2=0, fk-1=1;
fn=fn-1+fn-2+...+fn-k, n=k,k+1,...
举例说明:
(1)k=2时,即2阶裴波那契序列定义为:
f0=0, f1=1;
f2=f1+f0,
f3=f2+f1,
f4=f3+f2,
.....
(2)k=3时,即3阶裴波那契序列定义为:
f0=0, f1=0,f2=1;
f3=f2+f1+f0,
f4=f3+f2+f1,
f5=f4+f3+f2,
.....
*/
/**
* 动态规划求k阶裴波那契序列
*/
public class Fibonacci {
private int factor; //裴波那契序列阶数
private int length; //数列长度
private int [] fibo; //数列数组
/**
* length:数组大小
* factor:阶数(阶数大于1)
*/
public Fibonacci(int length,int factor){
if(factor<2){
System.out.println("阶数必须大于1");
factor=2;
}
this.factor=factor;
this.length=length;
this.fibo=new int[length];
for(int i=0;i<factor-1;i++){
this.fibo[i]=0;
}
this.fibo[factor-1]=1;
this.print();
}
/**
* pos:数列项数
*/
public int dp(int pos){
for(int i=this.factor;i<this.length;i++){
for(int j=i-1;j>=i-this.factor;j--){
this.fibo[i]+=this.fibo[j];
}
this.print();
if(i==pos-1){
return this.fibo[pos-1];
}
}
return 0;
}
/**
* 打印数组
*/
public void print(){
for(int i=0;i<this.fibo.length;i++){
System.out.print(this.fibo[i]+" ");
}
System.out.println();
}
public static void main(String [] args){
Fibonacci fino=new Fibonacci(20,4);
int value=fino.dp(15);
System.out.println(value);
}
}
/**
运行结果:
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 0 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 0 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 208 0 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 208 401 0 0 0 0 0 0
0 0 0 1 1 2 4 8 15 29 56 108 208 401 773 0 0 0 0 0
773
*/