HDU 5676 ztr loves lucky numbers

这题可以打表+二分,也可以直接构造答案输出。

打表+二分不多说了,暴力dfs一下就可以。

构造法:

如果输入的数字位数是奇数,那么输出比他多一位的最小值。例如输入12345,输出444777

剩下的情况都是位数是偶数的:

如果输入的数字比这个位数下能组成的最大值还大,那么输出多两位的最小值。例如,输入9999,输出444777

  剩下的情况都是用当前位数能构造出来的:从高位往低位构造,一位一位确定。

对于构造法不能AC的深表同情,后台数据有输入为0的,请直接输出47。

构造法:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

long long a[250];
long long ans[250];

int main()
{
    int T; scanf("%d", &T);
    while (T--)
    {
        long long n; scanf("%lld", &n);
        if (n == 0) { printf("47\n"); continue; }

        int len = 0; long long tmp = n;
        while (tmp>0) a[len++] = tmp % 10, tmp = tmp / 10;

        if (len % 2 == 1)
        {
            for (int i = 1; i <= (len + 1) / 2; i++) printf("4");
            for (int i = 1; i <= (len + 1) / 2; i++) printf("7");
            printf("\n"); continue;
        }

        else
        {
            long long num = 0;
            for (int i = 1; i <= len / 2; i++) num = num * 10 + 7;
            for (int i = 1; i <= len / 2; i++) num = num * 10 + 4;
            if (n>num)
            {
                for (int i = 1; i <= len / 2 + 1; i++) printf("4");
                for (int i = 1; i <= len / 2 + 1; i++) printf("7");
                printf("\n"); continue;
            }
            else
            {
                int ss = len / 2, sq = len / 2, u = 0;
                for (int i = len - 1; i >= 0; i--)
                {
                    if (a[i]<4)
                    {
                        if (ss>0) ans[u++] = 4, ss--;
                        else ans[u++] = 7, sq--;
                        break;
                    }
                    else if (a[i] == 4)
                    {
                        long long b = 0;
                        if (ss>0)
                        {
                            ans[u++] = 4; ss--;
                            for (int k = 0; k<u; k++) b = b * 10 + ans[k];
                            for (int k = 0; k<sq; k++)b = b * 10 + 7;
                            for (int k = 0; k<ss; k++)b = b * 10 + 4;
                            if (b<n) { ss++; ans[u - 1] = 7; sq--; break; }
                        }
                        else { ans[u++] = 7, sq--; break; }
                    }
                    else if (a[i]>4 && a[i]<7) { ans[u++] = 7; sq--; break; }
                    else if (a[i] == 7) { ans[u++] = 7; sq--; }
                }
                for (int k = 0; k<u; k++) printf("%lld", ans[k]);
                for (int k = 0; k<ss; k++) printf("4");
                for (int k = 0; k<sq; k++) printf("7");
                printf("\n");
            }
        }
    }
    return 0;
}

打表+二分:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const int maxn=1000000;
long long a[maxn];
int z;

void dfs(int tot,long long num,int si,int qi)
{
    if(num!=0&&si==qi&&tot%2==0) a[z++]=num;
    if(tot==18) return;
    dfs(tot+1,num*10+4,si+1,qi);
    dfs(tot+1,num*10+7,si,qi+1);
}

int main()
{
    z=0; dfs(0,0,0,0); sort(a,a+z);
    int T; scanf("%d",&T);
    while(T--)
    {
        long long n;  scanf("%lld",&n);
        if(n>777777777444444444)
        { printf("44444444447777777777\n");continue;}
        long long ans;
        int l=0,r=z-1;
        while(l<=r)
        {
            int mid=(l+r)/2;
            if(a[mid]>=n) r=mid-1,ans=a[mid];
            else l=mid+1;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

 

posted @ 2016-05-01 14:31  Fighting_Heart  阅读(201)  评论(0编辑  收藏  举报