(1)非递归
int main()
{
int n;
cin >> n;
int sum = 1; int b = 1; int t;
if (n == 1 || n == 2)
cout<< 1;
if (n > 2)
{
for (int i = 3; i <= n; i++)
{
t = sum;
sum = (sum + b) % 10007;
b = t;
}
cout << sum;
}
return 0;
}
(2)递归(运行超时30分)
int Fibonacci(int n)
{
if (n == 1 || n == 2)
return 1;
if (n < 1)
return -1;
if (n > 2)
{
return Fibonacci(n - 1) + Fibonacci(n - 2);
}
}
int main()
{
int n;
cin >> n;
cout << Fibonacci(n)%10007;
}