题目大意:给出一个数字,求不小于它且由4,7两个数字组成,4跟7的出现次数要一样多。
思路:DFS
每次递归累计4,7出现的个数,直到满足题意即可。
注意要long long
代码:
1
#include <iostream>
2
#include <cstdio>
3
#include <cstring>
4
#include <string>
5
#include <cmath>
6
#include <algorithm>
7
8
using namespace std;
9
10
long long n,res;
11
12
void dfs(long long x,int four,int seven)
13

{
14
if(x>1000000000000)
15
return;
16
if(x>=n&&four==seven)
17
{
18
if(res==0||res>x)
19
res=x;
20
}
21
dfs(x*10+4,four+1,seven);
22
dfs(x*10+7,four,seven+1);
23
}
24
25
int main()
26

{
27
while(~scanf("%I64d",&n))
28
{
29
res=0;
30
dfs(4,1,0);
31
dfs(7,0,1);
32
printf("%I64d\n",res);
33
}
34
return 0;
35
}
36