4424. David’s Lucky Numbers

来源:互联网 发布:c语言中问号的用法 编辑:程序博客网 时间:2024/04/29 17:34

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;}


原创粉丝点击