标签:amp div ret closed algorithm algo cstring max else
迭代加深
//Twenty
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
int n,num[1000],lim;
int dfs(int cnt,int x) {
if(num[cnt]==n) return 1;
if(cnt>=lim) return 0;
x=max(x,num[cnt]);
if(x*(1<<(lim-cnt))<n) return 0;
for(int i=0;i<=cnt;i++) {
num[cnt+1]=num[cnt]+num[i];
if(dfs(cnt+1,x)) return 1;
if(num[cnt]>num[i])
num[cnt+1]=num[cnt]-num[i];
else num[cnt+1]=num[i]-num[cnt];
if(dfs(cnt+1,x)) return 1;
}
return 0;
}
int main()
{
for(;;) {
scanf("%d",&n);
if(!n) break;
if(n==1) printf("0\n");
else{
num[0]=1;
for(lim=1;;lim++)
if(dfs(0,1)) break;
printf("%d\n",lim);
}
}
return 0;
}
标签:amp div ret closed algorithm algo cstring max else
原文地址:http://www.cnblogs.com/Achenchen/p/7507168.html