求斐波那契数列第n项,n<30,斐波那契数列前10项为 1,1,2,3,5,8,13,21,34,55
public static void main(String[] args) {
int a=1,b=0;
for (int i = 0; i < 30; i++) { //进行30次循环
System.out.println(a); //输出a的值,a的值即为数列的数
a=a+b; //可以看出第n项等于前两项之和
b=a-b; //b为前一项
}
}
结果:1,1 ,2,3,5,8,13,21,34.55,89,144,233,377,610,987,1597,2584,4181,6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040. |
|