Lucky Numbers (easy) CodeForces - 96B
Posted 啦啦啦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lucky Numbers (easy) CodeForces - 96B相关的知识,希望对你有一定的参考价值。
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn‘t contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.
Lucky number is super lucky if it‘s decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.
One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.
Input
The only line contains a positive integer n (1?≤?n?≤?109). This number doesn‘t have leading zeroes.
Output
Output the least super lucky number that is more than or equal to n.
Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.
Example
4500
4747
47
47
1 #include<cstdio> 2 #include<string> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 typedef long long ll; 8 9 const long long int INF=10000000000000; 10 11 ll n,ans,mcnt; 12 char vis[12][2]; 13 int a[12][2]={{4,7},{4,7},{4,7},{4,7},{4,7},{4,7},{4,7},{4,7},{4,7},{4,7},{4,7},{4,7}}; 14 15 16 void DFS(ll p,ll q,int cnt,int x,int y) 17 { ll tem=p*10+q; 18 if(tem>=n&&x==y) ans=min(ans,tem); 19 if(cnt==mcnt||cnt>12) return; 20 21 for(int i=0;i<2;i++){ 22 if(vis[cnt][i]) continue; 23 vis[cnt][i]=true; 24 25 if(a[cnt][i]==4) DFS(tem,a[cnt][i],cnt+1,x+1,y); 26 if(a[cnt][i]==7) DFS(tem,a[cnt][i],cnt+1,x,y+1); 27 vis[cnt][i]=false; 28 } 29 } 30 31 int main() 32 { while(cin>>n){ 33 memset(vis,false,sizeof(vis)); 34 mcnt=0,ans=INF; 35 int nn=n; 36 while(nn){ 37 nn=nn/10; 38 mcnt++; 39 } 40 if(mcnt%2==0) mcnt+=2; 41 if(mcnt%2!=0) mcnt+=1; 42 DFS(0,0,0,0,0); 43 cout<<ans<<endl; 44 } 45 return 0; 46 }
以上是关于Lucky Numbers (easy) CodeForces - 96B的主要内容,如果未能解决你的问题,请参考以下文章
Lucky Numbers (easy) CodeForces - 96B
1380. Lucky Numbers in a Matrix