Lucky Numbers (easy)
一道超级简单的BFS

#include<bits/stdc++.h> using namespace std; bool islucky(long long x){ int cnt4=0,cnt7=0; while(x){ int temp=x%10; if(temp==4){ cnt4++; } if(temp==7){ cnt7++; } x/=10; } if(cnt4==cnt7){ return true; } return false; } int main() { queue<long long> q; long long n; cin>>n; long long ans=0; q.push(ans);//记得放入队列 while(true){ ans=q.front(); q.pop(); if(ans>=n&&islucky(ans)){ cout<<ans; break; } q.push(ans*10+4); q.push(ans*10+7); } return 0; }

浙公网安备 33010602011771号