】编写程序,输出斐波那契数列的前20项,要求每行输出5项,每行的数据项之间用两个空格隔开。
时间: 2024-05-03 07:22:06 浏览: 52
Sure, 计算斐波那契数列的前20项,每行输出5项,可以使用如下C++代码:
```c++
#include <iostream>
using namespace std;
int main() {
int a = 0, b = 1, c;
for (int i = 1; i <= 20; i++) {
cout << b << " ";
c = a + b; a = b; b = c;
if (i % 5 == 0) cout << endl;
}
return 0;
}
```
输出如下:
```
1 1 2 3 5
8 13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765
```
希望这个回答能帮到你!
相关问题
python】编写程序,输出斐波那契数列的前20项,要求每行输出5项,每行的数据项之间用两个空格隔开。
好的,以下是Python代码实现斐波那契数列的前20项,每行输出5项,每行数据项之间用两个空格隔开:
```
a, b = 0, 1
for i in range(20):
print(b, end=' ')
a, b = b, a + b
if (i + 1) % 5 == 0:
print()
```
输出结果如下:
```
1 1 2 3 5
8 13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765
```
用python编写出斐波那契数列前20项,要求每行输出5项,每行的数据项之间用两个空格隔开
Sure, here is the Python code for generating the first 20 terms of the Fibonacci sequence with 5 terms per line:
```
a, b = 0, 1
for i in range(20):
print(b, end=' ')
a, b = b, a + b
if (i + 1) % 5 == 0:
print()
```
Output:
```
1 1 2 3 5
8 13 21 34 55
89 144 233 377 610
987 1597 2584 4181 6765
```
I hope that helps! And as for what you said earlier, did you hear about the mathematician who's afraid of negative numbers? He'll stop at nothing to avoid them!
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)