当前位置: 编程技术>java/j2ee
java实现fibonacci数列学习示例分享(斐波那契数列)
来源: 互联网 发布时间:2014-11-01
本文导语: 输出:1 1 2 3 5 代码如下:public class FibonaciTest { public static void main(String[] args) { Fibonaci(5); } public static void Fibonaci (int count) { int[] num = new int[count]; num[0] = num[1] = 1; for (int i = 2; i < count; i++) { num[i] = num[i - 1] + num...
输出:1 1 2 3 5
代码如下:
public class FibonaciTest {
public static void main(String[] args) {
Fibonaci(5);
}
public static void Fibonaci (int count) {
int[] num = new int[count];
num[0] = num[1] = 1;
for (int i = 2; i < count; i++) {
num[i] = num[i - 1] + num[i - 2];
}
for (int j = 0; j < count; j++){
System.out.print(num[j] + " ");
}
}
}