CF96B Lucky Number

题目描述
Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 44 and 77 . For example, numbers 4747 , 744744 , 44 are lucky and 55 , 1717 , 467467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 44 and 77 . For example, numbers 4747 , 77447744 , 474477474477 are super lucky and 44 , 744744 , 467467 are not.

One day Petya came across a positive integer nn . Help him to find the least super lucky number which is not less than nn .

输入格式
The only line contains a positive integer nn ( 1<=n<=10^9). This number doesn't have leading zeroes.

输出格式
Output the least super lucky number that is more than or equal to nn .

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.

题意翻译
super lucky number是只含有 44,77 且 44 的位数等于 77 的位数的数字;寻找大于等于 n(1<= n<= 10^9)的最小super lucky number。

输入输出样例
输入 #1

4500

输出 #1

4747

输入 #2

47

输出 #2

47

直接打表做就可以

涉及到几个知识点

next_permutation()函数

这个函数就是一个全排列的函数

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[3]={1,2,3};
	do
	{
		cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;
	}while(next_permutation(a,a+3));
	return 0;
}

输出

如果把next_permutation(a,a+3)改为(a,a+2)的话就是

此时就只对前两个数进行全排列

我们先写个程序,把所有的lucky number找出来

#include<iostream>
#include<algorithm>
using namespace std;
int a[500]={};
int main()
{
	for(int i=1;i<=5;i++)//这个就是根据输入的数字范围来确定,因为n要<=10^9
	{
                int t=0;
		for(int j=0;j<i;j++)
		{
			a[t++]=4;
		}
		for(int j=0;j<i;j++)//这里的循环就是生成47,4477,444777……这些数
		{
			a[t++]=7;
		}
		do
		{
			for(int k=0;k<i*2;k++)
			{
				cout<<a[k];
			}
			cout<<",";
		}while(next_permutation(a,a+2*i));//这一段就是对47,4477,444777……来进行全排列,省的我们一个一个去找	
	}
}

最后找到的数字

47,74,4477,4747,4774,7447,7474,7744,444777,447477,447747,447774,474477,474747,474774,477447,477474,477744,744477,744747,744774,747447,747474,747744,774447,774474,774744,777444,44447777,44474777,44477477,44477747,44477774,44744777,44747477,44747747,44747774,44774477,44774747,44774774,44777447,44777474,44777744,47444777,47447477,47447747,47447774,47474477,47474747,47474774,47477447,47477474,47477744,47744477,47744747,47744774,47747447,47747474,47747744,47774447,47774474,47774744,47777444,74444777,74447477,74447747,74447774,74474477,74474747,74474774,74477447,74477474,74477744,74744477,74744747,74744774,74747447,74747474,74747744,74774447,74774474,74774744,74777444,77444477,77444747,77444774,77447447,77447474,77447744,77474447,77474474,77474744,77477444,77744447,77744474,77744744,77747444,77774444,4444477777,4444747777,4444774777,4444777477,4444777747,4444777774,4447447777,4447474777,4447477477,4447477747,4447477774,4447744777,4447747477,4447747747,4447747774,4447774477,4447774747,4447774774,4447777447,4447777474,4447777744,4474447777,4474474777,4474477477,4474477747,4474477774,4474744777,4474747477,4474747747,4474747774,4474774477,4474774747,4474774774,4474777447,4474777474,4474777744,4477444777,4477447477,4477447747,4477447774,4477474477,4477474747,4477474774,4477477447,4477477474,4477477744,4477744477,4477744747,4477744774,4477747447,4477747474,4477747744,4477774447,4477774474,4477774744,4477777444,4744447777,4744474777,4744477477,4744477747,4744477774,4744744777,4744747477,4744747747,4744747774,4744774477,4744774747,4744774774,4744777447,4744777474,4744777744,4747444777,4747447477,4747447747,4747447774,4747474477,4747474747,4747474774,4747477447,4747477474,4747477744,4747744477,4747744747,4747744774,4747747447,4747747474,4747747744,4747774447,4747774474,4747774744,4747777444,4774444777,4774447477,4774447747,4774447774,4774474477,4774474747,4774474774,4774477447,4774477474,4774477744,4774744477,4774744747,4774744774,4774747447,4774747474,4774747744,4774774447,4774774474,4774774744,4774777444,4777444477,4777444747,4777444774,4777447447,4777447474,4777447744,4777474447,4777474474,4777474744,4777477444,4777744447,4777744474,4777744744,4777747444,4777774444,7444447777,7444474777,7444477477,7444477747,7444477774,7444744777,7444747477,7444747747,7444747774,7444774477,7444774747,7444774774,7444777447,7444777474,7444777744,7447444777,7447447477,7447447747,7447447774,7447474477,7447474747,7447474774,7447477447,7447477474,7447477744,7447744477,7447744747,7447744774,7447747447,7447747474,7447747744,7447774447,7447774474,7447774744,7447777444,7474444777,7474447477,7474447747,7474447774,7474474477,7474474747,7474474774,7474477447,7474477474,7474477744,7474744477,7474744747,7474744774,7474747447,7474747474,7474747744,7474774447,7474774474,7474774744,7474777444,7477444477,7477444747,7477444774,7477447447,7477447474,7477447744,7477474447,7477474474,7477474744,7477477444,7477744447,7477744474,7477744744,7477747444,7477774444,7744444777,7744447477,7744447747,7744447774,7744474477,7744474747,7744474774,7744477447,7744477474,7744477744,7744744477,7744744747,7744744774,7744747447,7744747474,7744747744,7744774447,7744774474,7744774744,7744777444,7747444477,7747444747,7747444774,7747447447,7747447474,7747447744,7747474447,7747474474,7747474744,7747477444,7747744447,7747744474,7747744744,7747747444,7747774444,7774444477,7774444747,7774444774,7774447447,7774447474,7774447744,7774474447,7774474474,7774474744,7774477444,7774744447,7774744474,7774744744,7774747444,7774774444,7777444447,7777444474,7777444744,7777447444,7777474444,7777744444

后面就比较简单了,只要找出那个刚好比n大的lucky number就好了

#include<iostream>
using namespace std;
long long a[500]={47,74,4477,4747,4774,7447,7474,7744,444777,447477,447747,447774,474477,474747,474774,477447,477474,477744,744477,744747,744774,747447,747474,747744,774447,774474,774744,777444,44447777,44474777,44477477,44477747,44477774,44744777,44747477,44747747,44747774,44774477,44774747,44774774,44777447,44777474,44777744,47444777,47447477,47447747,47447774,47474477,47474747,47474774,47477447,47477474,47477744,47744477,47744747,47744774,47747447,47747474,47747744,47774447,47774474,47774744,47777444,74444777,74447477,74447747,74447774,74474477,74474747,74474774,74477447,74477474,74477744,74744477,74744747,74744774,74747447,74747474,74747744,74774447,74774474,74774744,74777444,77444477,77444747,77444774,77447447,77447474,77447744,77474447,77474474,77474744,77477444,77744447,77744474,77744744,77747444,77774444,4444477777,4444747777,4444774777,4444777477,4444777747,4444777774,4447447777,4447474777,4447477477,4447477747,4447477774,4447744777,4447747477,4447747747,4447747774,4447774477,4447774747,4447774774,4447777447,4447777474,4447777744,4474447777,4474474777,4474477477,4474477747,4474477774,4474744777,4474747477,4474747747,4474747774,4474774477,4474774747,4474774774,4474777447,4474777474,4474777744,4477444777,4477447477,4477447747,4477447774,4477474477,4477474747,4477474774,4477477447,4477477474,4477477744,4477744477,4477744747,4477744774,4477747447,4477747474,4477747744,4477774447,4477774474,4477774744,4477777444,4744447777,4744474777,4744477477,4744477747,4744477774,4744744777,4744747477,4744747747,4744747774,4744774477,4744774747,4744774774,4744777447,4744777474,4744777744,4747444777,4747447477,4747447747,4747447774,4747474477,4747474747,4747474774,4747477447,4747477474,4747477744,4747744477,4747744747,4747744774,4747747447,4747747474,4747747744,4747774447,4747774474,4747774744,4747777444,4774444777,4774447477,4774447747,4774447774,4774474477,4774474747,4774474774,4774477447,4774477474,4774477744,4774744477,4774744747,4774744774,4774747447,4774747474,4774747744,4774774447,4774774474,4774774744,4774777444,4777444477,4777444747,4777444774,4777447447,4777447474,4777447744,4777474447,4777474474,4777474744,4777477444,4777744447,4777744474,4777744744,4777747444,4777774444,7444447777,7444474777,7444477477,7444477747,7444477774,7444744777,7444747477,7444747747,7444747774,7444774477,7444774747,7444774774,7444777447,7444777474,7444777744,7447444777,7447447477,7447447747,7447447774,7447474477,7447474747,7447474774,7447477447,7447477474,7447477744,7447744477,7447744747,7447744774,7447747447,7447747474,7447747744,7447774447,7447774474,7447774744,7447777444,7474444777,7474447477,7474447747,7474447774,7474474477,7474474747,7474474774,7474477447,7474477474,7474477744,7474744477,7474744747,7474744774,7474747447,7474747474,7474747744,7474774447,7474774474,7474774744,7474777444,7477444477,7477444747,7477444774,7477447447,7477447474,7477447744,7477474447,7477474474,7477474744,7477477444,7477744447,7477744474,7477744744,7477747444,7477774444,7744444777,7744447477,7744447747,7744447774,7744474477,7744474747,7744474774,7744477447,7744477474,7744477744,7744744477,7744744747,7744744774,7744747447,7744747474,7744747744,7744774447,7744774474,7744774744,7744777444,7747444477,7747444747,7747444774,7747447447,7747447474,7747447744,7747474447,7747474474,7747474744,7747477444,7747744447,7747744474,7747744744,7747747444,7747774444,7774444477,7774444747,7774444774,7774447447,7774447474,7774447744,7774474447,7774474474,7774474744,7774477444,7774744447,7774744474,7774744744,7774747444,7774774444,7777444447,7777444474,7777444744,7777447444,7777474444,7777744444};
long long n;
int main()
{
	cin>>n;
	for(int i=0;i<500;i++)
	{
		if(a[i]>=n)
		{
			cout<<a[i]<<endl;
			return 0;	
		}	
	}
	return 0;
}

posted @ 2022-03-03 16:03  Jinx8823  阅读(490)  评论(0)    收藏  举报