题目大意:

用最小的步数算出  x^n

思路:

直接枚举有限步数可以出现的所有情况。

然后加一个A*   就是如果这个数一直平方  所需要的步骤数都不能达到最优   就剪掉

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std; int n;
int save[1005]={1};
int h(int val)
{
if(val==0)return 0x3f3f3f3f; int cnt=0;
while(val<n)
{
val*=2;
cnt++;
}
return cnt;
}
bool dfs(int dep,int lit,int top)
{
if(dep>lit)return false; for(int i=0;i<top;i++)
{
save[top]=save[top-1]+save[i]; if(save[top]==n)return true;
if(dep+h(save[top])>lit)continue;
if(dfs(dep+1,lit,top+1))return true; save[top]=abs(save[top-1]-save[i]);
if(save[top]==n)return true;
if(dep+h(save[top])>lit)continue;
if(dfs(dep+1,lit,top+1))return true;
}
return false;
}
int main()
{
while(scanf("%d",&n)!=EOF && n)
{
save[0]=1;
if(n==1)printf("0\n");
else
for(int lit=1;;lit++)
{
if(dfs(1,lit,1))
{
printf("%d\n",lit);
break;
}
}
}
return 0;
}

最新文章

  1. MongoDB【第一篇】MongodDB初识
  2. Codeforces Round #342 (Div. 2) B. War of the Corporations(贪心)
  3. osgi dm
  4. 本人经过测试认为最简单最好的popupwindow样式
  5. 用c#开发微信 (13) 微统计 - 阅读分享统计系统 3 UI设计及后台处理
  6. (*p)++和*(p++)和*p++的区别
  7. Hadoop MapReduceV2(Yarn) 框架简介[转]
  8. photosop快速对白色背景图片进行抠图
  9. ASCII 码表对照
  10. mongodb中分页显示数据集的学习
  11. AospExtended K3 Note最新官方版 Android7.1.2 极速 省电 流畅 Galaxy XIAOMI Moto Lenovo Coolpad 均支持
  12. Node做中转服务器,转发接口
  13. 基于Python的数据分析(3):文件和时间
  14. 转:图像处理、显示中的行宽(linesize)、步长(stride)、间距(pitch)
  15. Python基础04_str_方法
  16. Python 3.4:Chromedrive,IEDriverServer,geckoDriver
  17. animate is not a function(zepto 使用报错)[转]
  18. sublime text 3中文乱码问题解决的方法
  19. HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)
  20. 用Docker自动构建纸壳CMS

热门文章

  1. 【英语】Bingo口语笔记(54) - how to date a foreigner
  2. Android Traceroute 功能实现
  3. requirejs之demo (转)
  4. Web Developer可以做得更多
  5. propertyGrid控件 z
  6. 初学JavaScript(入门一)
  7. C语言实现strcmp
  8. static用法总结
  9. C语言部分
  10. LeetCode Database: Consecutive Numbers