UVA-1374 Power Calculus (迭代加深搜索)
2015-09-22 10:04
453 查看
题目大意:问最少经过几次乘除法可以使x变成xn。
题目分析:迭代加深搜索。
代码如下:
打表版本:
题目分析:迭代加深搜索。
代码如下:
# include<iostream> # include<cstdio> # include<set> # include<cstring> # include<algorithm> using namespace std; int num[20],maxn,cnt; bool dfs(int d,int maxd,int n) { if(d==maxd) return num[cnt-1]==n; if((num[cnt-1]<<(maxd-d))<n) return false; for(int i=0;i<cnt;++i){ num[cnt]=num[cnt-1]+num[i]; ++cnt; if(num[cnt-1]<=1000&&dfs(d+1,maxd,n)) return true; --cnt; num[cnt]=num[cnt-1]-num[i]; ++cnt; if(num[cnt-1]>0&&dfs(d+1,maxd,n)) return true; --cnt; } return false; } int main() { int n; while(scanf("%d",&n)&&n) { for(int maxd=0;;++maxd){ cnt=0; num[cnt++]=1; if(dfs(0,maxd,n)){ printf("%d\n",maxd); break; } } } return 0; }
打表版本:
# include<iostream> # include<cstdio> # include<set> # include<cstring> # include<algorithm> using namespace std; int ans[1000]={0,1,2,2,3,3,4,3,4,4,5,4,5,5,5,4,5,5,6,5,6,6,6,5,6,6,6,6,7,6,6,5,6,6, 7,6,7,7,7,6,7,7,7,7,7,7,7,6,7,7,7,7,8,7,8,7,8,8,8,7,8,7,7,6,7,7,8,7,8,8,8,7,8,8,8,8, 8,8,8,7,8,8,8,8,8,8,9,8,9,8,9,8,8,8,8,7,8,8,8,8,9,8,9,8,9,9,9,8,9,9,9,8,9,9,9,9,9,9,9,8, 9,9,9,8,9,8,8,7,8,8,9,8,9,9,9,8,9,9,9,9,9,9,9,8,9,9,9,9,9,9,10,9,9,9,9,9,9,9,9, 8,9,9,9,9,9,9,10,9,10,9,10,9,10,10,10,9,10,10,10,9,10,10,10,9,10,9,10,9,9,9,9,8, 9,9,9,9,10,9,10,9,10,10,10,9,10,10,10,9,10,10,10,10,10,10,10,9,10,10,10,10,10,10, 10,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,9,10,10,10,10,10,10,10,9,10,10, 10,9,10,9,9,8,9,9,10,9,10,10,10,9,10,10,11,10,11,10,10,9,10,10,11,10,11,10,10,10, 10,10,10,10,10,10,10,9,10,10,10,10,10,10,11,10,10,10,11,10,11,11,11,10,11,10,11,10, 11,10,11,10,11,10,10,10,10,10,10,9,10,10,10,10,10,10,11,10,11,10,11,10,11,11,11,10,11,11,11, 10,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11, 10,11,11,11,10,11,11,11,10,11,10,11,10,10,10,10,9,10,10,10,10,11,10,11,10,11,11,11,10,11, 11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,11, 11,11,11,11,11,11,11,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,10,11,11,11,11,11, 11,11,11,11,11,11,11,12,11,12,11,11,11,12,11,12,11,11,11,11,11,11,11,11,11,11,10,11,11, 11,11,11,11,11,11,11,11,12,11,12,11,11,10,11,11,12,11,12,11,11,10,11,11,11,10,11,10,10,9,10, 10,11,10,11,11,11,10,11,11,12,11,12,11,11,10,11,11,12,11,12,12,11,11,12,12,12,11,12,11,11,10, 11,11,12,11,12,12,12,11,11,12,12,11,12,11,12,11,11,11,12,11,12,11,11,11,12,11,11,11,11,11,11,10,11, 11,11,11,11,11,12,11,11,11,12,11,12,12,12,11,12,11,12,11,12,12,12,11,12,12,12,12,12, 12,12,11,12,12,12,11,12,12,12,11,12,12,12,11,12,12,12,11,12,12,12,11,12,11,12,11, 12,11,11,11,11,11,11,10,11,11,11,11,11,11,12,11,12,11,12,11,12,12, 12,11,12,12,12,11,12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,11,12,12, 12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,11,12,12,12,11,12, 11,12,11,11,11,11,10,11,11,11,11,12,11,12,11,12,12,12,11,12,12,12,11,12,12,12,12, 12,12,12,11,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,13,12,12,12, 12,11,12,12,12,12,13,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,13,12,12,12,13,12,13,12,12,12,13,12,12,12,12,12,12,11,12,12,12,12,12, 12,13,12,12,12,13,12,13,12,12,12,13,12,13,12,13,12,12,12,12,12,12,12,12,12, 12,11,12,12,12,12,12,12,12,12,12,12,13,12,13,12,13,12,13,12,13,12,13,12,13,12,13, 13,13,12,13,13,13,12,13,12,13,12,13,13,13,12,13,13,13,12,13,12,13,12,12,12,13,12, 13,12,12,12,12,12,12,12,12,12,12,11,12,12,12,12,12,12,12,12,12,12,13,12,13,12, 12,12,12,12,13,12,13,13,13,12,13,13,13,12,13,12,12,11,12,12,13,12,13,13,13,12}; int main() { int n; while(scanf("%d",&n)&&n) printf("%d\n",ans[n-1]); return 0; }
相关文章推荐
- <s:iterator>的status属性
- bug整理
- maven 安装后,运行 mvn -v 出现异常
- win7 VMware CentOS桥接(bridge)模式网络配置
- win7 VMware CentOS桥接(bridge)模式网络配置
- 状态栏显示进度加载
- 实现了一下Mp3播放器的功能
- tomcat的启动时间
- Java字符串存储问题
- 常用iOS第三方库以及XCode插件介绍
- eclipse中启动tomcat时-超时
- 敏捷开发流程总结
- iOS NSLog各种打印
- Storeboard故事版使用小技巧
- jQuery easyUI 授权树实现后台代码
- 在线查看android源代码的3种方式
- 纯CSS绘制三角形(多个角度)
- codeforces 455B(博弈+dp)
- php 文件断点续传
- awk手册