很简单的题,就是模拟一下
#include<bits/stdc++.h> using namespace std; #define ll long long ll n,a[100],len; int f(int x,int y){ ll b[100],flag=0,res=0; memcpy(b,a,sizeof a); int pos; for(int i=1;i<=len;i++) if(b[i]==y){ pos=i;flag=1;break; } if(flag==0)return 0x3f3f3f3f; flag=0; for(int i=pos;i>1;i--) swap(b[i],b[i-1]),res++; for(int i=2;i<=len;i++) if(b[i]==x){ pos=i;flag=1;break; } if(flag==0)return 0x3f3f3f3f; flag=0; for(int i=pos;i>2;i--) swap(b[i],b[i-1]),res++; if(b[len]==0){ for(int i=len-1;i>=3;i--) if(b[i]!=0){ pos=i;flag=1;break; } if(flag==0)return 0x3f3f3f3f; for(int i=len;i>pos;i--) swap(b[i],b[i-1]),res++; } return res; } int main(){ cin>>n; while(n){ a[++len]=n%10; n/=10; } int ans=0x3f3f3f3f; ans=min(ans,f(0,0)); ans=min(ans,f(2,5)); ans=min(ans,f(5,0)); ans=min(ans,f(7,5)); if(ans!=0x3f3f3f3f) cout<<ans; else cout<<-1; }