请教一个问,写了斐波那契数列,想显示第80项,尝试了改为long没有效果,该怎么改进呢?谢谢大家!

#include <iostream>
using namespace std;

long f(int n)
{
    long f , g, v;
    f = g = 1;

    if (n == 0 || n == 1)
        return 1;
    else 
        while ( 0 < n--)
        {
            v = f + g;
            g = f;
            f = v;
        }
    return g;
}

int main()
{
    int n;
    cin >> n;

    while ( !(0 <= n && n <= 80) )
    {
        printf("重新输入!\n");
        cin >> n;
    }

    cout << f(n) << "\n";

    system("pause");
    return 0;
}

char数组, 可以显示到100位

如果感觉效率差可以用动态规划改一下

#include <iostream>
#include <algorithm>
using namespace std;

class Number {
private:
    char storage[100];
    int length;

public:
    Number(int n) {
        for (int i = 0; i < 100; i++) {
            this->storage[i] = 0;
        }
        this->length = 0;

        int cnt = 0;
        for (; n > 0; cnt++) {
            this->storage[cnt] = n % 10;
            n = (n - this->storage[cnt]) / 10;
        }
        this->length = cnt;
    }

    int GetLength() {
        return this->length;
    }

    int GetDigit(int index) {
        return this->storage[index];
    }

    void Print() {
        for (int i = this->length - 1; i >= 0; i--) {
            cout << int(this->storage[i]);
        }
        cout << endl;
    }

    void Add(Number that) {
        int length = max(this->length, that.GetLength());
        for (int i = 0; i < length; i++) {
            int sum = this->storage[i] + that.GetDigit(i);
            if (sum < 10) {
                this->storage[i] = sum;
            }
            else {
                this->storage[i] = sum - 10;
                this->storage[i + 1]++;
            }
        }

        for (int i = 99; i >= this->length; i--) {
            if (this->storage[i] != 0) {
                this->length = i + 1;
                break;
            }
        }

    }

};

void f(int n) {
    if (n == 0 || n == 1) {
        cout << 1 << endl;
        return;
    }

    auto a = Number(1);
    auto b = Number(1);
    for (int i = 1; i < n; i++) {
        if (i % 2 == 1) {
            a.Add(b);
        }
        else {
            b.Add(a);
        }
    }

    if (n % 2 == 0) {
        a.Print();
    }
    else {
        b.Print();
    }
}


int main()
{
    for (int i = 0; i < 80; i++) {
        f(i);
    }
}

可以尝试写一个高精度加法,不是很难写

一般都会用递归,你的算法,试试把while ( 0 < n--)改成while ( 2 < n--)

你好

首先用long在linux下编译执行后是可以的

其次我发现你这个程序,输入n项,算出的结果是n-1加n项的和,即第n+1项

所以直接输入80,会得到37889062373143906,那么这个数字是第八十一项

你这个是想显示第八十项的数值的话,参考如下

long f(int n)
{
    long f , g, v,p;
    f = g = 1;

    if (n == 0 || n == 1)
        return 1;
    else 
        while ( 0 < n--)
        {
            v = f + g;
                        p = g;
            g = f;
            f = v;
        }
    return p;
}

int main()
{
    int n;
    cin >> n;

    while ( !(0 <= n && n <= 81) )
    {
        printf("重新输入!\n");
        cin >> n;
    }

    cout << f(n) << "\n";

    system("pause");
    return 0;
}

这时候输入80即为第八十项的数值

您好!
谢谢您的回答!
因为有个第0项的存在,所以全部往后了一项;
为什么我在windows上不可以显示这么长的数值呢,哪里需要改吗?

1.如果要80位的话,可以用long long,很多机器上int和long的数据范围是一样的。
2.如果要显示再大一点的数,可以参考一下我的博客:https://blog.csdn.net/weixin_41461277/article/details/84937945https://blog.csdn.net/weixin_41461277/article/details/84843656