UVA - 1374 - Power Calculus(迭代加深)
2016-05-29 13:02
260 查看
从小到大枚举可能的次数,然后迭代加深搜索验证
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#include<ctime>
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long llu;
const int MAXN = 1000 + 10;
const int MAXT = 1000 + 10;
const int INF = 0x7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-6;
using namespace std;
int a[MAXN], ans, n;
bool dfs(int lur){
int pre = a[lur - 1];
if(lur == ans){
if(pre == n) return true;
return false;
}
if((pre << (ans - lur)) < n) return false;
for(int i = 0; i < lur; ++i){
a[lur] = pre + a[i];
if(a[lur] <= 1000 && dfs(lur + 1)) return true;
a[lur] = pre - a[i];
if(a[lur] > 0 && dfs(lur + 1)) return true;
}
return false;
}
int main(){
while(scanf("%d", &n) == 1 && n){
a[0] = 1;
for(ans = 1; !dfs(1); ++ans);
printf("%d\n", ans - 1);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
#include<ctime>
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef unsigned long long llu;
const int MAXN = 1000 + 10;
const int MAXT = 1000 + 10;
const int INF = 0x7f7f7f7f;
const double pi = acos(-1.0);
const double EPS = 1e-6;
using namespace std;
int a[MAXN], ans, n;
bool dfs(int lur){
int pre = a[lur - 1];
if(lur == ans){
if(pre == n) return true;
return false;
}
if((pre << (ans - lur)) < n) return false;
for(int i = 0; i < lur; ++i){
a[lur] = pre + a[i];
if(a[lur] <= 1000 && dfs(lur + 1)) return true;
a[lur] = pre - a[i];
if(a[lur] > 0 && dfs(lur + 1)) return true;
}
return false;
}
int main(){
while(scanf("%d", &n) == 1 && n){
a[0] = 1;
for(ans = 1; !dfs(1); ++ans);
printf("%d\n", ans - 1);
}
return 0;
}
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法
- mysql 模糊搜索的方法介绍
- C#搜索文字在文件及文件夹中出现位置的方法