题解 | #小招喵跑步#
小招喵跑步
https://www.nowcoder.com/practice/1177e9bd1b5e4e00bd39ca4ea9e4e216
#include <iostream> #include <cmath> using namespace std; int main() { int n; cin >> n; if (n == 0) cout << 0 << endl; else { n = abs(n); int dp[n + 1]; dp[0] = 0; dp[1] = 1; for(int i=2;i<=n;i++){ if(i % 2 == 0) dp[i] = dp[i / 2] + 1; else dp[i] = min(dp[i-1], dp[(i + 1) / 2] + 1) + 1; } cout << dp[n] << endl; } return 0; }