uva1374
#include <stdio.h>
const int N = 100;
int x, s[N];
bool dfs(int cur, int num, int pre) {//当前深度、最大深度、上次结束后的值
if (cur == num && pre == x)
return true;
if (cur >= num || pre > 1000 || pre <= 0)
return false;
if (pre << (num - cur) < x)//到不了x了
return false;
for (int i = 0; i < cur; i++) {
s[cur] = s[i] + pre;//先加
if (dfs(cur + 1, num, s[cur]))
return true;
s[cur] = pre - s[i];//再减
if (dfs(cur + 1, num, s[cur]))
return true;
}
return false;
}
int main() {
while (scanf("%d", &x) == 1 && x) {
s[0] = 1;
for (int i = 1;; i++) {
if (dfs(1, i, 1)) {
printf("%d\n", i - 1);
break;
}
}
}
return 0;
}