CF 96b
题意
定义 Lucky_number 是一个只含有 4 和 7 的数,且 4 和 7 的数量相等
给定一个 n ,求大于等于 n 的最小 Lucky_number
分析
bfs即可,每次向后添加一个 4 或 7
检查合不合法即可
代码
#include <bits/stdc++.h>
using namespace std;
bool com(string a , string b)
{
if(a.length() & 1)
{
return false;
}
int cnt = 0;
for(int i = 0 ; i < a.length() ; i++)
{
if(a[i] == '4')
{
cnt++;
}
else
{
cnt--;
}
}
if(cnt != 0)
{
return false;
}
return (a.length() == b.length() ? a >= b : a.length() > b.length());
}
string ch_(string s)
{
queue <string> q;
q.push("4");
q.push("7");
while(!q.empty())
{
string x = q.front();
q.pop();
if(com(x , s))
{
return x;
}
q.push(x + "4");
q.push(x + "7");
}
}
int main()
{
string s;
cin>>s;
cout<<ch_(s);
return 0;
}