88的雷达9

yang_zhiyu 2023-11-18 10:22:57

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;

int f[1000010];

int dfs(int n)
{
	if(f[n]!=-1)
	{
		return f[n];
	}
	if(n==1)
	{
		return 0;
	}
	if(n%2==0)
	{
		return 1+dfs(n/2); 
	}
	else
	{
		return f[n]=min(1+dfs(n-1),1+dfs(n+1)); 
	}
}

int main()
{
	int n;
	cin>>n;
	memset(f,-1,sizeof(f));
	cout<<dfs(n)<<endl;
	return 0;
}