- 上台阶
我不理解为啥递归就超时,之前的那个斐波那契也是一样超时(上台阶)
- 2021-10-16 16:59:55 @
#include<stdio.h>
long long f(int n){
if(n <= 2){
return n;
}
return f(n-1) + f(n-2);
}
int main()
{
int x;
scanf("%d",&x);
printf("%lld",f(x));
return 0;
} //这个超时了```
```c
#include<stdio.h>
int main()
{
long long f1,f2;
f1=1;
f2=2;
long long x;
long long n;
long long i;
scanf("%lld",&n);
for(i=0;i<n-2;i++)
{
x=f2;
f2=f1+f2;
f1=x;
}
if(n==1)f2=1;
printf("%lld\n",f2);
return 0;
} //这个没超时```
4 comments
-
mibbp Acmer LV 10 MOD @ 2021-10-18 12:35:35
能递推不递归
-
2021-10-16 22:29:44@
相同数据规模下,递归本来就比迭代慢吧
-
2021-10-16 19:37:24@
好啦好啦,看吧看吧
-
2021-10-16 17:55:39@
写帖子时旁边不是有Markdown教程嘛,用代码标志来写,不然这样你的代码好难看,很不容易看懂
- 1
Information
- ID
- 49
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 538
- Accepted
- 135
- Uploaded By