【CodeForce】520-B Two Buttons

题目链接

题意很简单,但是不知道为什么正着做做不出来,逆向思维的话就很简单了。
n能乘2能加1相当于m能除2能减1。

AC代码:

#include<iostream>
using namespace std;

int dfs(int n,int m){
    int total = 0;
    if(n>=m)    return n-m;
    while(n!=m){
        if(n<m){
            if(m%2==0){
                m /= 2;
                total++;
            }
            else{
                m = (m+1)/2;
                total+=2;
            }
        }
        else{
            m++;
            total++;
        }
    }
    return total;
}
int main(void){
    int n,m;
    cin>>n>>m;
    cout<<dfs(n,m)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41009682/article/details/80791652
今日推荐