标签:ace 需要 new mes pac 编程 col ++ out
小招喵喜欢在数轴上跑来跑去,假设它现在站在点n处,它只会3种走法,分别是:
1.数轴上向前走一步,即n=n+1
2.数轴上向后走一步,即n=n-1
3.数轴上使劲跳跃到当前点的两倍,即n=2*n
现在小招喵在原点,即n=0,它想去点x处,快帮小招喵算算最快的走法需要多少步?
#include <iostream> //#include <algorithm> using namespace std; #define min(a, b) (((a)<(b))?(a):(b)) int minSteps(int n) { int result = 0; if (n < 0) n = -1 * n; //动态规划方法 if (n == 0) return 0; if (n == 1) return 1; int *steps = new int[n + 1]; steps[0] = 0; steps[1] = 1; for (int i = 2; i <= n; i++) { if (i % 2 == 0) steps[i] = min(steps[i - 1], steps[i / 2]) + 1; else steps[i] = min(steps[i - 1], steps[(i + 1) / 2] + 1) + 1; } result = steps[n]; delete[]steps; return result; } int main() { int n; cin >> n; cout << minSteps(n) << endl; return 0; }
标签:ace 需要 new mes pac 编程 col ++ out
原文地址:https://www.cnblogs.com/wqpkita/p/9559081.html