思路:只能对n进行减1和乘2的操作来得到m,模拟一下即可,我是将m进行加1和除2的操作得到n;
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
int n, m, ans;
while(scanf("%d %d", &n, &m) != EOF) {
if(m <= n) {
printf("%d\n", n - m); continue;
}
ans = 0;
while(n != m) {
int p = (m + 1) / 2;
if(p < n) {
if(m & 1) ans++;
ans += (n - p);
ans++;
break;
}
else if(p >= n) {
if(m & 1) ans++;
ans++;
m = (m + 1) / 2;
}
}
printf("%d\n", ans);
}
}