编写一个C++风格的程序,计算输出Fibonacci数列的前20项

编写一个C++风格的程序,计算输出Fibonacci数列的前20项

#include<iostream>
using namespace std;
int main()
{
    int *p = new int[20];
    *p = 1;
    *(p + 1) = 1;
    cout<<*p<<"\t"<<*(p + 1)<<"\t";
    p = p + 2;
    for (int i = 3; i <= 20; i++)
    {   

        *(p) = *(p - 1) + *(p - 2);
        cout<<*p<<"\t";
        p++;
        if (i%5 == 0)
            cout<<"\n";
    }
    return 0;
}

总结:
1.创建了一个20个元素的整型数组形式为new int[20];
2.转义字符的输出也要加上双引号如”\t”;
3.局部变量int i;不能在for循环里面定义,要在括号内定义并初始化如:for(int i=3; ; )这可能与编译器有关;
4.”\t”的使用:使光标向后移动8个字符的位置输出数据。

    原文作者:舞伴问题
    原文地址: https://blog.csdn.net/qq_29486343/article/details/46947057
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞