日期:2014-05-20  浏览次数:20689 次

菲波纳数列
public class FeiBo {
public static void Xiang(int n) {
int sum = 1;
int a0 = 0;
int a1 = 1;
if (n < 2) {
System.out.println(a1);
} else {
int an = a(n - 1) + a(n - 2);
for (int i = 2; i <= n; i++) {
sum += ai;
System.out.println(sum);
}
}
}
public static void main(String[] args) {
Xiang(1);
}
};

ai 怎么定义?


------解决方案--------------------
好像是 a[i] 吧
Java code

    public static void main(String args[]){    
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] fb = new int[10000];
    fb[0] = 1;
    fb[1] = 1;
    for(int i = 0; i < n; i++){
        fb[i + 2] = fb[i] + fb[i + 1];
    }         
    System.out.println(fb[n - 1]);
    }
//----------------------------------------------------
    public static void main(String args[]){  
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    System.out.println(fb(n - 1));
    }
    public static int fb(int n){
    if(n == 1 || n == 0){
        return 1;
    }else{
        return fb(n - 1) + fb(n - 2);
    }
    }