C - Lucky Numbers (easy)

题目链接https://vjudge.net/contest/207453#problem/C

题目大意:给任一数字n,找一个数x。

x满足:1.>=n,且最小;2.仅由4,7组成,且4和7的个数相等。

数据范围(1 ≤ n ≤ 109). 

解题思路:读完题第一想法是暴力,直接循环,寻找符合条件的x,但超时,于是有了打表的想法,打表程序就是暴力找满足条件的所有数,一共350个,打表程序时间小于2分钟。emmm,打表。。。有点low,于是想到了位运算,不超时,但用不惯,终于。。。发现这个题直接递归就好(感谢某位美女让我帮忙查代码)。

递归代码如下:

#include<iostream>
using namespace std;
long long n, ans = 1LL << 60;
void F(long long x, int y, int z) {//通过x构造答案,y是4的个数,z是7点个数 
	if(x >= n && y == z) ans=min(ans, x);//答案ans满足要求:1.大于等于n  2.只存在7和4,且7的个数等于4的个数
	if(x > n * 100) return;//答案肯定在n和n*100之间,所以x>n*100的时候退出; 
	F(x * 10 + 4, y + 1, z);//当前答案x加一位数4 
	F(x * 10 + 7, y, z + 1);//当前答案x加一位数7
}
int main() {
	cin >> n;
	F(0, 0, 0);
	cout << ans;
	return 0;}
打表代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long s[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 l;
int main()
{
	cin>>l;
	for(int i=0;;i++)
	{
		if(s[i]>=l)
		{cout<<s[i];
		return 0;
		}
	}
}
    
打表过程代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
long long s[500];
int l;
bool sc(long long x)
{
	int a,b,c;
	b=c=0;
	while(x)
	{
		a=x%10;
		x/=10;
		if(a!=4&&a!=7)return 0;
		if(a==7)b++;
		else c++;
	} 
	if(b==c)return 1;
	else return 0;
}
int main()
{
	freopen("ss.out","w",stdout);
	/*s[1]=47;s[2]=74;s[3]=4477;
	s[4]=4747;s[5]=4774;l=5;*/
/*	for(int i=7447;i<=7744;i++)
	{
		if(sc(i))cout<<i<<",";
	}
	for(int i=444777;i<=477744;i++)
	{
		if(sc(i))cout<<i<<",";
	}
	for(int i=744477;i<=777444;i++)
	{
		if(sc(i))cout<<i<<",";
	}
	for(int i=44447777;i<=47777444;i++)
	{
		if(sc(i))cout<<i<<",";
	}
for(int i=74444777;i<=77774444;i++)
	{
		if(sc(i))cout<<i<<",";
	}*/
	for(long long i=4444477777;i<=4777774444;i++)
	{
	if(sc(i))cout<<i<<",";
	}
	for(long long i=7444447777;i<=7777744444;i++)
	{
		if(sc(i))cout<<i<<",";
	} 
    
}

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值