当前位置: 移动技术网 > 移动技术>移动开发>Android > 【 CodeForces - 96 B 】 Lucky Numbers (easy) 暴力打表

【 CodeForces - 96 B 】 Lucky Numbers (easy) 暴力打表

2020年07月08日  | 移动技术网移动技术  | 我要评论

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的数

思路:

逐位模拟那可太麻烦了。观察这种数只能是偶数长度,那就直接把2,4,6, 8,10长度的只含4、7且数量相同的数全部找出来就完事了。而且找完发现也就才350个。。。所以就先打表,然后每个case里面lowerbound查找就A了。属实水题。。

AC代码:

#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e5+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'|ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0',  ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };

ll a[maxn];
int p = 0;

void dfs(int pos, int len, int num1, int num2, ll sum)
{
    if(pos>len)
    {
        if(num1 == num2) a[p++] = sum;
        return ;
    }
    dfs(pos+1,len,num1+1, num2, sum*10+4);
    dfs(pos+1,len,num1,num2+1,sum*10+7);
}

int main()
{
    for(int i=2;i <= 10; i += 2) dfs(1, i, 0, 0, 0LL);
    ll n;
    sort(a,a+p);
    while(cin>>n)
    {
        int idx = lower_bound(a,a+p, n) - a;
        cout<<a[idx]<<'\n';
    }
    return 0;
}

本文地址:https://blog.csdn.net/qq_45492531/article/details/107168624

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网