1. 程式人生 > >C - Lucky Numbers (easy)

C - Lucky Numbers (easy)

一位數 main class 大於等於 學習 ase res itl for

Problem description

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.

Examples

Input
4500
Output
4747
Input
47
Output
47
解題思路:題目要求輸出不小於n的最小整數,這個整數要求只含有4和7這兩個數字,並且4出現的次數等於7出現的次數。暴力打表(大概2分鐘),水過!
打表代碼:
 1 #include<iostream>
 2
using namespace std; 3 typedef long long LL; 4 bool judge(LL x){ 5 int t1 = 0, t2 = 0; 6 while (x){ 7 int a = x % 10; 8 if (a != 4 && a != 7)return false; 9 if (a == 4)t1++; 10 if (a == 7)t2++; 11 x /= 10; 12 } 13 if (t1 == t2)return true; 14 else return false; 15 } 16 int main(){ 17 for (LL i = 47; i < 10000000000; ++i) 18 if (judge(i)){ cout << i << ,; } 19 return 0; 20 }
AC代碼:
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 LL n,obj[]={47,74,4477,4747,4774,7447,7474,7744,
 5         444777,447477,447747,447774,474477,474747,474774,
 6         477447,477474,477744,744477,744747,744774,747447,
 7         747474,747744,774447,774474,774744,777444,44447777,
 8         44474777,44477477,44477747,44477774,44744777,44747477,
 9         44747747,44747774,44774477,44774747,44774774,44777447,
10         44777474,44777744,47444777,47447477,47447747,47447774,
11         47474477,47474747,47474774,47477447,47477474,47477744,
12         47744477,47744747,47744774,47747447,47747474,47747744,
13         47774447,47774474,47774744,47777444,74444777,74447477,
14         74447747,74447774,74474477,74474747,74474774,74477447,
15         74477474,74477744,74744477,74744747,74744774,74747447,
16         74747474,74747744,74774447,74774474,74774744,74777444,
17         77444477,77444747,77444774,77447447,77447474,77447744,
18         77474447,77474474,77474744,77477444,77744447,77744474,
19         77744744,77747444,77774444,4444477777};
20 int main(){
21     cin>>n;
22     for(int i=0;;++i)
23         if(obj[i]>=n){cout<<obj[i]<<endl;break;}
24     return 0;
25 }

再貼另一種很好理解的遞歸寫法(反正我沒想到QAQ,不過代碼思路真的很溜,值得學習一下,漲一下見識)AC代碼

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 LL n,ans=1LL <<60;
 5 void f(LL x,int y,int z){//通過x構造答案,y是4的個數,z是7點個數
 6     if(x>=n && y==z)ans=min(ans,x);//答案ans滿足要求:1.大於等於n  2.只存在7和4,且7的個數等於4的個數
 7     if(x>n*100)return;//答案肯定在n和n*100之間,所以x>n*100的時候退出;
 8     f(x*10+4,y+1,z);//當前答案x加一位數4
 9     f(x*10+7,y,z+1);//當前答案x加一位數7
10 }
11 int main(){
12     cin>>n;
13     f(0,0,0);
14     cout<<ans<<endl;
15     return 0;
16 }

C - Lucky Numbers (easy)