tle求助
查看原帖
tle求助
817033
ikun_czy楼主2023/7/27 18:36
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<math.h>
using namespace std;
int main(void)
{
    short n;
    cin>>n;
    //bool f[]={};
    //f[0]=1;
    //f[1]=1;
    if(n==1)cout<<0;
    queue<short>q1;
    queue<short>q2;
    q1.push(1);
    q2.push(0);
    while(!q1.empty())
    {
        short k1=q1.front(),k2=q2.front();
        q1.pop();
        q2.pop();
        //cout<<k1<<' '<<k2<<endl;
        if(k1==n)
        {
            cout<<k2;
            return 0;
        }
        if(k1*2<=n)
        {
            q1.push(k1*2);
            q2.push(k2+1);    
        }
        if(k1+1<=n)
        {
            q1.push(k1+1);
            q2.push(k2+1);    
        }
        if(k1-1)
        {
            q1.push(k1-1);
            q2.push(k2+1);    
        }
    }
    return 0;
}
2023/7/27 18:36
加载中...