• 一道经典B(打)F(表)S(题)之“Find The Multiple”


    题目:

       Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1.

       You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.

     

       The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.

       For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.

      Sample Input

        2

        6

        19

        0

      Sample Output

        10

        100100100100100100

        111111111111111111

    题目大意:

      输入一个不超过200的整数 n,找出一个整数 m 使得 m 是 n 的整数倍,而且 m 只能由 0 和 1 组成。

    解题思路:

      一道经典的BFS。

      一开始我们可能会认为这道题的结果很大,long 型存不下,但是我用程序实际跑了一下,发现 long 是能存下的,c++应该是unsign long long能存下。

      所以就交了几次,一发超时,一发超内存。。。。十分尴尬。

      然后我就特么的 打了个表。。。了个表。。。个表。。。表。。。

    超内存代码(BFS思想,java语言):

     1 import java.util.*;
     2 import java.math.BigInteger;
     3 public class Main{
     4 
     5     public static void main(String[] args){
     6         Scanner sc = new Scanner(System.in);
     7         while(sc.hasNext()){
     8             int n = sc.nextInt();
     9             if(n == 0){break;}
    10             Queue<String> m = new LinkedList<String>();
    11             m.offer("1");
    12             long p = Long.valueOf(m.peek());
    13             while(p % n != 0){
    14                 String t = m.peek();
    15                 m.poll();
    16                 m.offer(t + "0");
    17                 m.offer(t + "1");
    18                 p = Long.valueOf(m.peek());
    19             }    
    20             System.out.println(m.peek());
    21         }
    22     }
    23 }

    AC代码(打表):

      1 import java.util.*;
      2 import java.math.BigInteger;
      3 public class POJ1426{
      4     
      5     static String list[] = {
      6         "1",
      7         "10",
      8         "111",
      9         "100",
     10         "10",
     11         "1110",
     12         "1001",
     13         "1000",
     14         "111111111",
     15         "10",
     16         "11",
     17         "11100",
     18         "1001",
     19         "10010",
     20         "1110",
     21         "10000",
     22         "11101",
     23         "1111111110",
     24         "11001",
     25         "100",
     26         "10101",
     27         "110",
     28         "110101",
     29         "111000",
     30         "100",
     31         "10010",
     32         "1101111111",
     33         "100100",
     34         "1101101",
     35         "1110",
     36         "111011",
     37         "100000",
     38         "111111",
     39         "111010",
     40         "10010",
     41         "11111111100",
     42         "111",
     43         "110010",
     44         "10101",
     45         "1000",
     46         "11111",
     47         "101010",
     48         "1101101",
     49         "1100",
     50         "1111111110",
     51         "1101010",
     52         "10011",
     53         "1110000",
     54         "1100001",
     55         "100",
     56         "100011",
     57         "100100",
     58         "100011",
     59         "11011111110",
     60         "110",
     61         "1001000",
     62         "11001",
     63         "11011010",
     64         "11011111",
     65         "11100",
     66         "100101",
     67         "1110110",
     68         "1111011111",
     69         "1000000",
     70         "10010",
     71         "1111110",
     72         "1101011",
     73         "1110100",
     74         "10000101",
     75         "10010",
     76         "10011",
     77         "111111111000",
     78         "10001",
     79         "1110",
     80         "11100",
     81         "1100100",
     82         "1001",
     83         "101010",
     84         "10010011",
     85         "10000",
     86         "1111111101",
     87         "111110",
     88         "101011",
     89         "1010100",
     90         "111010",
     91         "11011010",
     92         "11010111",
     93         "11000",
     94         "11010101",
     95         "1111111110",
     96         "1001",
     97         "11010100",
     98         "10000011",
     99         "100110",
    100         "110010",
    101         "11100000",
    102         "11100001",
    103         "11000010",
    104         "111111111111111111",
    105         "100",
    106         "101",
    107         "1000110",
    108         "11100001",
    109         "1001000",
    110         "101010",
    111         "1000110",
    112         "100010011",
    113         "110111111100",
    114         "1001010111",
    115         "110",
    116         "111",
    117         "10010000",
    118         "1011011",
    119         "110010",
    120         "1101010",
    121         "110110100",
    122         "10101111111",
    123         "110111110",
    124         "100111011",
    125         "111000",
    126         "11011",
    127         "1001010",
    128         "10001100111",
    129         "11101100",
    130         "1000",
    131         "11110111110",
    132         "11010011",
    133         "10000000",
    134         "100100001",
    135         "10010",
    136         "101001",
    137         "11111100",
    138         "11101111",
    139         "11010110",
    140         "11011111110",
    141         "11101000",
    142         "10001",
    143         "100001010",
    144         "110110101",
    145         "100100",
    146         "10011",
    147         "100110",
    148         "1001",
    149         "1111111110000",
    150         "11011010",
    151         "100010",
    152         "1100001",
    153         "11100",
    154         "110111",
    155         "11100",
    156         "1110001",
    157         "11001000",
    158         "10111110111",
    159         "10010",
    160         "1110110",
    161         "1010100",
    162         "10101101011",
    163         "100100110",
    164         "100011",
    165         "100000",
    166         "11101111",
    167         "11111111010",
    168         "1010111",
    169         "1111100",
    170         "1111110",
    171         "1010110",
    172         "11111011",
    173         "10101000",
    174         "10111101",
    175         "111010",
    176         "1111011111",
    177         "110110100",
    178         "1011001101",
    179         "110101110",
    180         "100100",
    181         "110000",
    182         "100101111",
    183         "110101010",
    184         "11010111",
    185         "11111111100",
    186         "1001111",
    187         "10010",
    188         "100101",
    189         "110101000",
    190         "1110",
    191         "100000110",
    192         "1001011",
    193         "1001100",
    194         "1010111010111",
    195         "110010",
    196         "11101111",
    197         "111000000",
    198         "11001",
    199         "111000010",
    200         "101010",
    201         "110000100",
    202         "1101000101",
    203         "1111111111111111110",
    204         "111000011",
    205         "1000"
    206     };
    207     public static void main(String[] args){
    208         Scanner sc = new Scanner(System.in);
    209         while(sc.hasNext()){
    210             int n = sc.nextInt();
    211             if(n == 0){break;}
    212             System.out.println(list[n - 1]);
    213         }
    214     }
    215 }

    至于C++版本的代码,博主博客不再发布了,毕竟大多数人用C++,网上很多解题报告任君选择。

  • 相关阅读:
    Vim自动补全神器YouCompleteMe的配置
    sudo apt-get update 时出现的hit、ign、get的含义
    C语言可变参数宏及‘##’在可变参数中的作用
    搬书游戏
    程序中数字取反结果公式
    利用宏定义中的##实现函数模板的作用
    C中的空宏定义,即只有一个参数
    浏览器缓存技术介绍
    Vue相关原理以及手写一个MyVue
    详解JS中的prototype、__proto__与constructor(图解)
  • 原文地址:https://www.cnblogs.com/love-fromAtoZ/p/7551264.html
Copyright © 2020-2023  润新知