2177: Lucky Numbers (easy)

本文介绍了一个算法问题,即寻找大于等于给定数的最小“超幸运数”。超幸运数是一种特殊的正整数,其十进制表示仅包含4和7,并且4和7的数量相等。文中提供了一种解决方案,通过生成所有可能的超幸运数并对其进行排序来找到符合条件的最小数。

题目链接

Description

B. Lucky Numbers (easy)
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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

题目的意思就是给出一个数找出大于等于这个数的最小数(这个数由4和7组成)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s1[1000],s2="";
    int flag=0;
    for(int i=1;i<6;i++)
    {
        s2.clear();
        for(int j=0;j<i;j++)
            s2.push_back('4');
        for(int j=0;j<i;j++)
            s2.push_back('7');
        do
        {
            s1[flag++]=s2;
            //cout<<s1[flag-1]<<endl;
        }while(next_permutation(s2.begin(),s2.end()));
        //cout<<flag<<endl;

    }
    long long int a[1000];
    for(int i=0;i<flag;i++)
    {
            sscanf(s1[i].c_str(),"%lld",&a[i]);
    }
    long long int n;
    cin>>n;
    printf("%lld\n",a[lower_bound(a,a+flag,n)-a]);
    return 0;
}

分析:

a.用到了next_permutation() 这个是全排列一个用法,以前没用过

  使用方法就是上述代码的形式,

b.用到了string 原本自己用的是vector但是有个问题就是把一维数组赋值给二维数组是错误的

c. s.c_str()就是为了把c++和c兼容而搞出来的这里的sscanf()是c里的

  所以要转换一下

int: +-2147483647  十位 这里最大是5个7和5个4所以用long long int 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值