essential c++ 练习个人解决方案--2.1

essential c++ 练习个人解决方案--2.1,第1张

#include
#include
using namespace std;
bool fibon_elem(int pos, int& elem)
{
    if (pos <= 0 || pos > 1024)
    {
        elem = 0;return false;
    }//判断是否符合范围
    elem = 1;
    int n_2 = 1, n_1 = 1;
    for (int ix = 3;ix <= pos;++ix) {
        elem = n_2 + n_1;
        n_2 = n_1;n_1 = elem;
    }//得到指定的elem
    return true;
}
int main()
{
    int elem, i = 0;int a;
    int pos[100];
    while (1) {

        cin >> pos[i++];//有循环的话,输入的数就会分段进去比如1 2 3 4就是先1进去得到结果,再2 再3
        if (fibon_elem(pos[i - 1], elem))
        {
            cout << elem << " ";
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/674906.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-19
下一篇 2022-04-19

发表评论

登录后才能评论

评论列表(0条)

保存