20pt求助
查看原帖
20pt求助
317400
Foggy_night楼主2024/1/6 22:22
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 1e6+5;
int f[N], n;
int dfs(int x){
	if(x < 1 || x > n)
		return 0x3f3f3f3f;
	if(x == n)	
		return 0;
	if(f[x] != -1)
		return f[x];
	f[x] = 0x3f3f3f3f;
	f[x] = min(f[x], dfs(x-1)+1);
	f[x] = min(f[x], dfs(x+1)+1);
	f[x] = min(f[x], dfs(2*x)+1);
	return f[x]; 
}
int main(){
	scanf("%d", &n);
	memset(f, -1, sizeof(f));
	printf("%d", dfs(1));
	return 0;
} 
2024/1/6 22:22
加载中...