1、数组打印斐波那契数列
时间: 2023-11-15 08:05:19 浏览: 54
好的,这个问题我可以回答。以下是一个打印斐波那契数列的数组代码:
```python
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [1]
elif n == 2:
return [1, 1]
else:
fib_list = [1, 1]
for i in range(2, n):
fib_list.append(fib_list[i-1] + fib_list[i-2])
return fib_list
print(fibonacci(10))
```
这个代码可以打印出前10个斐波那契数列。
相关问题
1、数组打印斐波那契数列c++
Sure, I can help you with that! Here's some code to print the Fibonacci sequence in an array using C:
```
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of terms in the Fibonacci sequence: ");
scanf("%d", &n);
int fib[n];
fib[0] = 0;
fib[1] = 1;
for(i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
printf("The Fibonacci sequence is: ");
for(i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
printf("\n");
return 0;
}
```
I hope that helps!
数组打印斐波那契数列c++
好的,这是一个关于编程的问题。以下是用 C 语言打印斐波那契数列的代码:
```c
#include <stdio.h>
int main() {
int n, i;
int fib[100];
printf("请输入要打印的斐波那契数列的项数:\n");
scanf("%d", &n);
fib[0] = 0;
fib[1] = 1;
for (i = 2; i < n; i++) {
fib[i] = fib[i-1] + fib[i-2];
}
printf("斐波那契数列的前 %d 项为:\n", n);
for (i = 0; i < n; i++) {
printf("%d ", fib[i]);
}
printf("\n");
return 0;
}
```
希望能够帮到您!
阅读全文