zoukankan      html  css  js  c++  java
  • HDU 1664 Different Digits

    Different Digits

    Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 464    Accepted Submission(s): 106

    Problem Description
    Given a positive integer n, your task is to find a positive integer m, which is a multiple of n, and that m contains the least number of different digits when represented in decimal. For example, number 1334 contains three different digits 1, 3 and 4.
     
    Input
    The input consists of no more than 50 test cases. Each test case has only one line, which contains a positive integer n ( 1<=n < 65536). There are no blank lines between cases. A line with a single `0' terminates the input.
     
    Output
    For each test case, you should output one line, which contains m. If there are several possible results, you should output the smallest one. Do not output blank lines between cases.
     
    Sample Input
    7
    15
    16
    101
    0
     
    Sample Output
    7
    555
    16
    1111
     
    Source
     
    Recommend
    xhd
     
    思路:根据抽屉原理,可以先搜只有一位数字的,然后从小到大搜索两位数字的
    但是我在搜索两位数字的时候,采用暴力搜索,结果毫无疑问的超时了,悲剧
    了,在3170上卡了很久,而且在以30,40,70,80,20,90,结尾时时间开销有点
     
    代码:
    超时(暴搜)
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    int n;
    int flag,temp,sum[100],step;
    int num[100];
    void BFS(int a)
    {
        int biji;
        int map[10000];
        int qmap = 0,p;
        while(a > 9)
        {
            p = a % 10;
            a = a / 10;
            map[qmap ++] = p;
        }
        map[qmap] = a;
        int mmm[10000];
        memset(mmm,0,sizeof(mmm));
        for(int q = 0;q <= qmap;q ++)
            mmm[q] = map[q];
        int shumu = 0;
        for(int k = 0;k <= 9;k ++)
                    num[k] = k;
        for(int l = 0;l <= qmap;l ++)
        {
            if(num[map[l]] != -1)
            {
                    shumu ++;
                    num[map[l]] = -1;
            }
        }
        if(shumu == 2)
        {
            for(int m = qmap;m >= 0;m --)
                    printf("%d",map[m]);
            printf(" ");
            return ;
        }
        while(1)
        {
            biji = 0;
            for(int j = 0;j <= qmap;j ++)
            {
                   map[j] = map[j] + mmm[j] + biji;
                   biji = map[j] / 10;
                   map[j] = map[j] % 10;    
            }
            while(biji > 10)
            {
               qmap ++;
               map[qmap] = biji % 10;
               biji = biji / 10;
            }
            if(biji)
            {
                qmap ++;
                map[qmap] = biji;
            }
            int shumu = 0;
            for(int k = 0;k <= 9;k ++)
                        num[k] = k;
            for(int l = 0;l <= qmap;l ++)
            {
                if(num[map[l]] != -1)
                {
                    shumu ++;
                    num[map[l]] = -1;
                }
            }
            if(shumu == 2)
            {
                for(int m = qmap;m >= 0;m --)
                       printf("%d",map[m]);
                printf(" ");
                return ;
            }
        }
    }
    int main()
    {
        //while(scanf("%d",&n),n != 0)
        for(int n = 3160;n <= 3180;n ++)
        {
            printf("%d ",n);
            flag = 0;
            temp = 0;
            memset(sum,0,sizeof(sum));
            for(int i = 1;i <= 100000;i ++)
            {
                for(int j = 1;j <= 9;j ++)
                {
                    temp = j;
                    sum[j] = sum[j] * 10 + j;
                    sum[j] = sum[j] % n;
                    if(sum[j] == 0 || sum[j] % n == 0)
                    {
                        flag = 1;
                        step = i;
                        break;
                    }
                }
                if(flag)
                  break;
            }
            if(flag)
            {
                for(int k = 1;k <= step;k ++)
                   printf("%d",temp);
                printf(" ");
            }
            else
            {
                BFS(n);
            }
        }
        return 0;
    }
  • 相关阅读:
    SVN--代码状态检查(图文并茂)
    SVN服务器的环境搭建(图文并茂)
    SVN基本配置--创建版本库(图文并茂)
    GPS定位(GPS导航)GPS授时三者是一个概念吗?
    北斗授时服务器(北斗时钟服务器)助力工业组态监控系统
    浅谈PTP精密时钟同步(GPS北斗授时)原理及应用
    NTP时间同步服务器(时钟服务器)如何助力桥梁监控系统的?
    网络时间服务器(NTP服务器)应用铁路计算机联锁系统
    NTP时钟服务器(时钟系统)助力阜阳卷烟厂
    子母钟系统(GPS授时系统)工作模式介绍
  • 原文地址:https://www.cnblogs.com/GODLIKEING/p/3301791.html
Copyright © 2011-2022 走看看