在计算机上运行以下程序:
public class Main{
public static long F(int N){
if (N == 0) return 0;
if (N == 1) return 1;
return F(N-1)+F(N-2);
}
public static void main(String[] args) {
//裴波那切数列
int[] f = new int[100];
f[0]=0;f[1]=1;
for (int N = 0; N < 100; N ++){
System.out.println(N+","+F(N));
}
}
}
改进程序
public class Main{
public static void main(String[] args) {
//裴波那切数列,用数组保存已经计算过的值
int[] f = new int[100];
f[0]=0;f[1]=1;
for (int N = 2; N < 100; N ++){
f[N]+=f[N-1]+f[N-2];
System.out.println(N+","+f[N]);
//System.out.println(N+","+F(N));
}
}
}