Home > Back-end >  The Fibonacci sequence problem
The Fibonacci sequence problem

Time:10-10

Submitted the blue cup system always can't through
using namespace std;
Int fun (int a, b int, int x, int n)
{
Int sum=a + b;
If (x==n) return the sum % 10007;
n++;
Fun (b, sum, x, n);
}
Int main ()
{
int x; Int z=1;
Cin & gt;> x;
If (x & lt;
=2){
Cout & lt; }
The else
{
If (x & gt; 2)
{
Int n=3;
Int c=fun (1, 1, x, n);
}
}
system("pause");
return 0;
}
The answer is 55 when n=10 output is 55 but also submit through
But the following code can ask bosses solutions
using namespace std;
Const int mod=10007;

Int main () {
Int num, fib, one=1, tow=1;
Cin & gt;> Num.
If (num & lt;
=2){
Cout & lt; return 0;
}
For (int I=2; i Fib=+ tow (one) % mod;
One %=tow mod;
Tow=fib % mod;
}
Cout & lt; system("pause");
return 0;
}

CodePudding user response:

The time complexity and space complexity are meet the requirements

CodePudding user response:

You don't heading file?

CodePudding user response:

The difference is that, the input data is very large, recursive calls are likely to be too deep, you cause stack overflow

CodePudding user response:

Int sum=a + b;
If (x==n) return the sum % 10007;
Your code is only when the x==n the sum modulo

Fib=+ tow (one) % mod;
One %=tow mod;
Tow=fib % mod;
Everywhere take over

Your code is also likely to cause sum addition to overflow, for a lot of data when

The number of this kind of game, all must consider scope

CodePudding user response:

Fun
reference 3 floor response:
the difference is that the input data is very large, recursive calls are likely to be too deep, you cause stack overflow
got it
  • Related