4424. David’s Lucky Numbers

探索数字世界的神秘,发现只包含4和7的幸运数字,如何找到大于或等于给定数值的最小超级幸运数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

4424. David’s Lucky Numbers

Description

David loves lucky numbers. In his opinion, 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 David came across a positive integer N. Help him to find the least super lucky number which is not less than N.

Input

The first line contains an integer T (1<=T<=10), indicating the number of test cases.

Then, for each case, there is only a number N (1<=N<=10^9).

 

Output

For each case, print the super Lucky number that is more than or equal to N in one line.

Sample Input

2
4500
47

Sample Output

4747
47

做复杂了,开个整形数组就可以的,悲剧啊

#include <iostream>
#include <string>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string strn;
        cin>>strn;
        int len=strn.length(),shu=0,maxshu=0;
        for(int i=0;i<len;i++)
            shu+=(strn[len-1-i]-'0')*pow(10.0,i);
        for(int i=0;i<(len-1)/2+1;i++)
            maxshu+=4*pow(10.0,i);
        for(int i=(len-1)/2+1;i<len;i++)
            maxshu+=7*pow(10.0,i);
        if(shu>maxshu)
        {
            for(int i=0;i<=len/2;i++)
                cout<<"4";
            for(int i=0;i<=len/2;i++)
                cout<<"7";
            cout<<endl;continue;
        }
        if(len%2)
        {
            for(int i=0;i<=len/2;i++)
                cout<<"4";
            for(int i=0;i<=len/2;i++)
                cout<<"7";
            cout<<endl;continue;
        }
        string s="00000000000";
        for(int i=0;i<len/2+1;i++)
            s[i]='4';
        for(int i=len/2;i<len;i++)
            s[i]='7';
        int a[11];
        for(int i=0;i<len;i++)
                a[i]=s[i]-'0';
        do
        {   
            int shu2=0;
            for(int i=0;i<len;i++)
                shu2+=a[i]*pow(10.0,len-i-1);
            if(shu<=shu2)
            {
                cout<<shu2<<endl;break;
            }
        }
        while(next_permutation(a,a+len));
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值