package com.kingdz.algorithm.time201706;
import java.util.Arrays;
/**
* <pre>
* 步骤问题
*
* http://judgecode.com/problems/1006
*
* Suppose you start from x = 0.
* In each step you can operate on x by either of the 2 operations:
* (1) x = x * 2
* (2) x = x + 1
* Given an integer n. How many operations at least to change x into n?
*
* Input:A single non-negative integer n which is no larger than 2147483647.
* Output:The minimum number of operations to change x = 0 into n.
* </pre>
*
* @author kingdz
*
*/
public class Algo22 {
public static void main(String[] args) {
int n = 100;
int[] arr = new int[n + 1];
arr[0] = 1;
for (int i = 0; i < arr.length; i++) {
int now = arr[i];
int a = i * 2;
int b = i + 1;
if (a < arr.length && arr[a] == 0) {
arr[a] = now + 1;
}
if (b < arr.length && arr[b] == 0) {
arr[b] = now + 1;
}
if (a == n || b == n) {
break;
}
}
System.out.println(arr[n] - 1);
System.out.println(Arrays.toString(arr));
}
}