C++ 斐波那契序列C++;显示一个/最大值

C++ 斐波那契序列C++;显示一个/最大值,c++,C++,字面上说,我被问到这个问题“斐波那契序列是0,1,1,2,3,5,8,13;前两项分别为0和1,其后的每一项都是前两项的总和,即Fib[n]=Fib[n–1]+Fib[n–2]。使用此信息,编写一个C++程序,计算Fibonacci序列中的第n个数,其中用户在程序中交互地输入N。例如,如果n=6,程序应显示值8。“ 谢谢你对上一个问题的回答,我已经把它写进了我的完整代码中。我确实有一个循环,这意味着用户可以选择是否继续该程序。它以前工作过,但现在什么也没发生。有人能解释一下吗?谢谢 {int N

字面上说,我被问到这个问题“斐波那契序列是0,1,1,2,3,5,8,13;前两项分别为0和1,其后的每一项都是前两项的总和,即Fib[n]=Fib[n–1]+Fib[n–2]。使用此信息,编写一个C++程序,计算Fibonacci序列中的第n个数,其中用户在程序中交互地输入N。例如,如果n=6,程序应显示值8。“

谢谢你对上一个问题的回答,我已经把它写进了我的完整代码中。我确实有一个循环,这意味着用户可以选择是否继续该程序。它以前工作过,但现在什么也没发生。有人能解释一下吗?谢谢

{int N;

char ans = 'C';

while (toupper(ans) == 'C')
{
    cout<<"This program is designed to give the user any value of the Fibonacci Sequence that they desire, provided the number is a positive integer.";//Tell user what the program does

    cout<<"\n\nThe formula of the Fibonacci Sequence is;   Fib[N] = Fib[N – 1] + Fib[N – 2]\n\n"; //Declare the Formula for the User

    cout<<"Enter a value for N, then press Enter:"; //Declare Value that the User wants to see

    cin>>N;//Enter the Number

    if (N>1) {
            long u = 0, v = 1, t;

            for(int Variable=2; Variable<=N; Variable++)
            {
                t = u + v;
                u = v;
                v = t;
            } //Calculate the Answer

        cout<<"\n\nThe "<<N<<"th Number of the Fibonacci Sequence is: "<<t; //Show the Answer
    }

    if (N<0) {
        cout<<"\n\nThe value N must be a POSITIVE integer, i.e. N > 0"; //Confirm that N must be a positive integer. Loop.
    }
    if (N>100) {
        cout<<"\n\nThe value for N must be less than 100, i.e. N < 100. N must be between 0 - 100.";//Confirm that N must be less than 100. Loop.
    }
    if (N==0) {
        cout<<"\n\nFor your value of N, \nFib[0] = 0"; //Value will remain constant throughout, cannot be caculated through formula. Loop.
    }
    if (N==1) {
        cout<<"\n\nFor your value of N. \nFib[1]=1";//Value will remain constant throughout, cannot be caculated through formula. Loop.
    }

  cout << "\n\nIf you want to select a new value for N, then click C then press Enter. If you want to quit, click P then press Enter: ";
    cin >> ans;
}


return 0;
{int N;
char ans='C';
while(toupper(ans)='C')
{

cout您只需在下面放置两行即可。您不需要额外的{},但不会造成伤害。

这是您的主循环:

for(int i=2; i<=N; i++)
{
    t = u + v;
    u = v;
    v = t;

    cout<<t<<"is your answer";
}
但它从未被定义或使用过。为什么这里有这个声明?(删除它)

您有一组冗余的大括号:

else {
        {
            unsigned long long u = 0, v = [....]

你不需要紧跟着大括号再加上更多大括号。

cout
移出循环。额外
{
在其他之后??好的,非常感谢!你能向我解释一下吗?只是想了解实际发生了什么…正如你所看到的,
cout
在你的循环中。因此,每个循环都计算下一个值并打印它。你只需要计算。你只打印最后一个值,在loo之后p、 再次感谢!更新了如上所示的完整代码,就我所见,仍然是一个小问题。问题是什么?现在可以了,很抱歉。出现的小问题是一些数字看起来是负数…但它似乎是一个随机的数字组合,例如100、193,而其他大多数都是正确的。(我将N的限制改为300)。我只是有一个问题,因为这是在我从等式中删除了‘无符号长’部分之后。这会影响负数吗?如果会,如何影响?我还有一个问题是关于“for(int Variable=2;Variable)的。谢谢,我现在已经整理好了。我还从“else”中删除了无符号长{{unsigned long long u=0,v=[…]因为它们似乎什么都不是。我从讲师的笔记中得到了这个函数……为什么没有区别?
unsigned long long Fib(int N);
else {
        {
            unsigned long long u = 0, v = [....]