java实现fibonacci数列学习示例分享(斐波那契数列)
程序员文章站
2024-02-19 19:24:10
输出:1 1 2 3 5
复制代码 代码如下:public class fibonacitest { publi...
输出: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] + " ");
}
}
}