变化的数字
2025-01-23 11:11:55
发布于:上海
#include<bits/stdc++.h>
using namespace std;
int dist[1000010];//把1变成i需要多少次操作
int main(){
int a,n;
cin>>a>>n;
queue<int> q;
for(int i=0;i<=1000000;i++)dist[i]=1e9;
q.push(1);
dist[1] = 0;
while(q.size()){
int top = q.front();
q.pop();
//cout<<top<<endl;
//操作1 *a
if((long long)top*a <= 1e6){
if(dist[top*a]>dist[top]+1){
dist[top*a]=dist[top]+1;
q.push(top*a);
}
}
//top>=10 && top%10!=0 操作2
if(top>=10 && top%10!=0){
// 10123
int copy = top;
int mod = top%10;
while(copy>0){
copy/=10;
mod*=10;
}
int res = top/10 + mod/10;
//cout<<top<<" "<<res<<endl;
if(res<=1e6){
if(dist[res]>dist[top]+1){
dist[res]=dist[top]+1;
q.push(res);
}
}
}
}
if(dist[n]==1e9)cout<<-1;
else cout<<dist[n];
return 0;
}
这里空空如也
有帮助,赞一个