hdu2041
来源:互联网 发布:ubuntu 12.04 配置dns 编辑:程序博客网 时间:2024/06/08 02:33
递归Time Limit Exceeded超时
#include<iostream>#include<math.h>#include<string>using namespace std;int step(int n){ if(n==1) return 1; if(n==2) return 2; return step(n-1)+step(n-2);}int main(){ int n,m; cin>>n; for(int i=0;i<n;i++) { cin>>m; cout<<step(m-1)<<endl; } return 0;}
设置断点就AC了
#include<iostream>#include<math.h>#include<string>using namespace std;int step(int n){ if(n==1) return 1; if(n==2) return 2; if(n==9) return 55; if(n==19) return 6765; if(n==29) return 832040; if(n==34) return 9227465; return step(n-1)+step(n-2);}int main(){ int n,m; cin>>n; for(int i=0;i<n;i++) { cin>>m; cout<<step(m-1)<<endl; } return 0;}
后来找了别人的用循环的
step[1] = 1; step[2] = 2; for(int i=3; i<m; i++) { step[i] = step[i-1] + step[i-2]; }
比完全递归快在时间复杂度少了一半。
还能少得更多吗,赋值过的是否还需再赋值。
#include<iostream>#include<math.h>#include<string>using namespace std;static int s[50];int step(int n){ if(n==1) return 1; if(n==2) return 2; if(s[n]==0) s[n]=step(n-1)+step(n-2); return s[n];}int main(){ int n,m; cin>>n; for(int i=0;i<n;i++) { cin>>m; cout<<step(m-1)<<endl; } return 0;}
0 0
- HDU2041
- hdu2041
- HDU2041
- hdu2041
- HDU2041
- hdu2041 2046
- hdu2041超级楼梯
- hdu2041(超级楼梯)
- hdu2041(超级楼梯)
- HDU2041:超级楼梯
- hdu2041 超级楼梯
- hdu2041 超级楼梯
- hdu2041超级阶梯
- HDU2041 超级楼梯
- hdu2041 超级楼梯
- hdu2041(递推)
- HDU2041超级楼梯
- HDU2041 简单递归
- Android 编程下 Touch 事件的分发和消费机制
- 蚂蚁
- python文件操作
- [LeetCode] Remove Duplicates from Sorted List II
- LeetCode Find Minimum in Rotated Sorted Array II 有条件的二分
- hdu2041
- Redis各种数据结构介绍以及相应的业务场景应用
- Kmeans算法寻找合适K值
- NYOJ 17 单调递增最长子序列 (二分 STL,lower_bound)
- 深入剖析U3D的Miss Mono Behaviour问题
- HTML 30分钟入门教程
- 第一章 原始字符串, 模块
- Unity代码混淆方案
- HDU2067 - 小兔的棋盘 (递推)