求菲波那契数列前n个数

img


试了很多种方法,但是不知道怎么实现五行一列输出,我感觉题目有点问题

#include <iostream>
using namespace std;
int main()
{
    int f1 = 1,f2 = 1,n;
    cin>>n;
    cout<<f1<<"\t"<<f2<<"\t";
    for(int i = 2; i < n; i++)
    {
        int f3 = f1 + f2;
        f1 = f2,f2 = f3;
        cout<<f2<<"\t";
        if((i + 1) % 5 == 0) cout<<endl;
    }
    return 0;
}

img


教材上确实漏洞百出,自己弄懂了就好

题目是有点问题, 没有输出前面的1
https://blog.csdn.net/weixin_35711852/article/details/117144282

f1=f2
f2=f1+f2
(i>0) & (i%5 == 0)


#include<stdio.h>
int main(void) {
 int i, n,x1,x2,x3 ;
 scanf("%d",&n);
 x1 = 1;
 x2 = 1;
 if(n == 1) {
  printf("%d ",x1);
 }
 if(n == 2) {
  printf("%d %d ",x1,x2);
 } else {
  printf("%d %d ",x1,x2);
  for(i = 1; i <= n; i++) {
   x3 = x2 + x1;
   printf("%d ",x3);
   x1 = x2;
   x2 = x3;
  }
 }
 return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632