洛谷B3636
B3636 文字工作 - 洛谷
代码区:
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e6;
int n,step[MAX];
queue<int> q;
void bfs(){
q.push(1);
while(q.size()){
int t=q.front();
q.pop();
if(t+1<=n&&!step[t+1]){
step[t+1]= step[t]+1;
q.push(t+1);
}
if(t*2<=n&&!step[t*2]){
step[2*t]=step[t]+1;
q.push(2*t);
}
}
}
int main(){
cin >> n;
bfs();
cout << step[n];
return 0;
}
欢迎各位读者提出意见。
(菜菜洛谷奋斗小日记)