Find The Multiple(POJ_1426)

190 篇文章 2 订阅
12 篇文章 0 订阅

题面

题意

给出一个n,输出n的倍数中一个仅有0和1构成的数,n<=200.

法1 打表

将长度在11及以内的01串打下来,并特判几个不在这个长度内的数
也可以直接打印200个数的答案表
非最优方法,但也是个解题技巧

代码

#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;

ll num[2000000]={1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111,10000,10001,10010,10011,10100,10101,10110,10111,11000,11001,11010,11011,11100,11101,11110,11111,100000,100001,100010,100011,100100,100101,100110,100111,101000,101001,101010,101011,101100,101101,101110,101111,110000,110001,110010,110011,110100,110101,110110,110111,111000,111001,111010,111011,111100,111101,111110,111111,1000000,1000001,1000010,1000011,1000100,1000101,1000110,1000111,1001000,1001001,1001010,1001011,1001100,1001101,1001110,1001111,1010000,1010001,1010010,1010011,1010100,1010101,1010110,1010111,1011000,1011001,1011010,1011011,1011100,1011101,1011110,1011111,1100000,1100001,1100010,1100011,1100100,1100101,1100110,1100111,1101000,1101001,1101010,1101011,1101100,1101101,1101110,1101111,1110000,1110001,1110010,1110011,1110100,1110101,1110110,1110111,1111000,1111001,1111010,1111011,1111100,1111101,1111110,1111111,10000000,10000001,10000010,10000011,10000100,10000101,10000110,10000111,10001000,10001001,10001010,10001011,10001100,10001101,10001110,10001111,10010000,10010001,10010010,10010011,10010100,10010101,10010110,10010111,10011000,10011001,10011010,10011011,10011100,10011101,10011110,10011111,10100000,10100001,10100010,10100011,10100100,10100101,10100110,10100111,10101000,10101001,10101010,10101011,10101100,10101101,10101110,10101111,10110000,10110001,10110010,10110011,10110100,10110101,10110110,10110111,10111000,10111001,10111010,10111011,10111100,10111101,10111110,10111111,11000000,11000001,11000010,11000011,11000100,11000101,11000110,11000111,11001000,11001001,11001010,11001011,11001100,11001101,11001110,11001111,11010000,11010001,11010010,11010011,11010100,11010101,11010110,11010111,11011000,11011001,11011010,11011011,11011100,11011101,11011110,11011111,11100000,11100001,11100010,11100011,11100100,11100101,11100110,11100111,11101000,11101001,11101010,11101011,11101100,11101101,11101110,11101111,11110000,11110001,11110010,11110011,11110100,11110101,11110110,11110111,11111000,11111001,11111010,11111011,11111100,11111101,11111110,11111111,100000000,100000001,100000010,100000011,100000100,100000101,100000110,100000111,100001000,100001001,100001010,100001011,100001100,100001101,100001110,100001111,100010000,100010001,100010010,100010011,100010100,100010101,100010110,100010111,100011000,100011001,100011010,100011011,100011100,100011101,100011110,100011111,100100000,100100001,100100010,100100011,100100100,100100101,100100110,100100111,100101000,100101001,100101010,100101011,100101100,100101101,100101110,100101111,100110000,100110001,100110010,100110011,100110100,100110101,100110110,100110111,100111000,100111001,100111010,100111011,100111100,100111101,100111110,100111111,101000000,101000001,101000010,101000011,101000100,101000101,101000110,101000111,101001000,101001001,101001010,101001011,101001100,101001101,101001110,101001111,101010000,101010001,101010010,101010011,101010100,101010101,101010110,101010111,101011000,101011001,101011010,101011011,101011100,101011101,101011110,101011111,101100000,101100001,101100010,101100011,101100100,101100101,101100110,101100111,101101000,101101001,101101010,101101011,101101100,101101101,101101110,101101111,101110000,101110001,101110010,101110011,101110100,101110101,101110110,101110111,101111000,101111001,101111010,101111011,101111100,101111101,101111110,101111111,110000000,110000001,110000010,110000011,110000100,110000101,110000110,110000111,110001000,110001001,110001010,110001011,110001100,110001101,110001110,110001111,110010000,110010001,110010010,110010011,110010100,110010101,110010110,110010111,110011000,110011001,110011010,110011011,110011100,110011101,110011110,110011111,110100000,110100001,110100010,110100011,110100100,110100101,110100110,110100111,110101000,110101001,110101010,110101011,110101100,110101101,110101110,110101111,110110000,110110001,110110010,110110011,110110100,110110101,110110110,110110111,110111000,110111001,110111010,110111011,110111100,110111101,110111110,110111111,111000000,111000001,111000010,111000011,111000100,111000101,111000110,111000111,111001000,111001001,111001010,111001011,111001100,111001101,111001110,111001111,111010000,111010001,111010010,111010011,111010100,111010101,111010110,111010111,111011000,111011001,111011010,111011011,111011100,111011101,111011110,111011111,111100000,111100001,111100010,111100011,111100100,111100101,111100110,111100111,111101000,111101001,111101010,111101011,111101100,111101101,111101110,111101111,111110000,111110001,111110010,111110011,111110100,111110101,111110110,111110111,111111000,111111001,111111010,111111011,111111100,111111101,111111110,111111111,1000000000,1000000001,1000000010,1000000011,1000000100,1000000101,1000000110,1000000111,1000001000,1000001001,1000001010,1000001011,1000001100,1000001101,1000001110,1000001111,1000010000,1000010001,1000010010,1000010011,1000010100,1000010101,1000010110,1000010111,1000011000,1000011001,1000011010,1000011011,1000011100,1000011101,1000011110,1000011111,1000100000,1000100001,1000100010,1000100011,1000100100,1000100101,1000100110,1000100111,1000101000,1000101001,1000101010,1000101011,1000101100,1000101101,1000101110,1000101111,1000110000,1000110001,1000110010,1000110011,1000110100,1000110101,1000110110,1000110111,1000111000,1000111001,1000111010,1000111011,1000111100,1000111101,1000111110,1000111111,1001000000,1001000001,1001000010,1001000011,1001000100,1001000101,1001000110,1001000111,1001001000,1001001001,1001001010,1001001011,1001001100,1001001101,1001001110,1001001111,1001010000,1001010001,1001010010,1001010011,1001010100,1001010101,1001010110,1001010111,1001011000,1001011001,1001011010,1001011011,1001011100,1001011101,1001011110,1001011111,1001100000,1001100001,1001100010,1001100011,1001100100,1001100101,1001100110,1001100111,1001101000,1001101001,1001101010,1001101011,1001101100,1001101101,1001101110,1001101111,1001110000,1001110001,1001110010,1001110011,1001110100,1001110101,1001110110,1001110111,1001111000,1001111001,1001111010,1001111011,1001111100,1001111101,1001111110,1001111111,1010000000,1010000001,1010000010,1010000011,1010000100,1010000101,1010000110,1010000111,1010001000,1010001001,1010001010,1010001011,1010001100,1010001101,1010001110,1010001111,1010010000,1010010001,1010010010,1010010011,1010010100,1010010101,1010010110,1010010111,1010011000,1010011001,1010011010,1010011011,1010011100,1010011101,1010011110,1010011111,1010100000,1010100001,1010100010,1010100011,1010100100,1010100101,1010100110,1010100111,1010101000,1010101001,1010101010,1010101011,1010101100,1010101101,1010101110,1010101111,1010110000,1010110001,1010110010,1010110011,1010110100,1010110101,1010110110,1010110111,1010111000,1010111001,1010111010,1010111011,1010111100,1010111101,1010111110,1010111111,1011000000,1011000001,1011000010,1011000011,1011000100,1011000101,1011000110,1011000111,1011001000,1011001001,1011001010,1011001011,1011001100,1011001101,1011001110,1011001111,1011010000,1011010001,1011010010,1011010011,1011010100,1011010101,1011010110,1011010111,1011011000,1011011001,1011011010,1011011011,1011011100,1011011101,1011011110,1011011111,1011100000,1011100001,1011100010,1011100011,1011100100,1011100101,1011100110,1011100111,1011101000,1011101001,1011101010,1011101011,1011101100,1011101101,1011101110,1011101111,1011110000,1011110001,1011110010,1011110011,1011110100,1011110101,1011110110,1011110111,1011111000,1011111001,1011111010,1011111011,1011111100,1011111101,1011111110,1011111111,1100000000,1100000001,1100000010,1100000011,1100000100,1100000101,1100000110,1100000111,1100001000,1100001001,1100001010,1100001011,1100001100,1100001101,1100001110,1100001111,1100010000,1100010001,1100010010,1100010011,1100010100,1100010101,1100010110,1100010111,1100011000,1100011001,1100011010,1100011011,1100011100,1100011101,1100011110,1100011111,1100100000,1100100001,1100100010,1100100011,1100100100,1100100101,1100100110,1100100111,1100101000,1100101001,1100101010,1100101011,1100101100,1100101101,1100101110,1100101111,1100110000,1100110001,1100110010,1100110011,1100110100,1100110101,1100110110,1100110111,1100111000,1100111001,1100111010,1100111011,1100111100,1100111101,1100111110,1100111111,1101000000,1101000001,1101000010,1101000011,1101000100,1101000101,1101000110,1101000111,1101001000,1101001001,1101001010,1101001011,1101001100,1101001101,1101001110,1101001111,1101010000,1101010001,1101010010,1101010011,1101010100,1101010101,1101010110,1101010111,1101011000,1101011001,1101011010,1101011011,1101011100,1101011101,1101011110,1101011111,1101100000,1101100001,1101100010,1101100011,1101100100,1101100101,1101100110,1101100111,1101101000,1101101001,1101101010,1101101011,1101101100,1101101101,1101101110,1101101111,1101110000,1101110001,1101110010,1101110011,1101110100,1101110101,1101110110,1101110111,1101111000,1101111001,1101111010,1101111011,1101111100,1101111101,1101111110,1101111111,1110000000,1110000001,1110000010,1110000011,1110000100,1110000101,1110000110,1110000111,1110001000,1110001001,1110001010,1110001011,1110001100,1110001101,1110001110,1110001111,1110010000,1110010001,1110010010,1110010011,1110010100,1110010101,1110010110,1110010111,1110011000,1110011001,1110011010,1110011011,1110011100,1110011101,1110011110,1110011111,1110100000,1110100001,1110100010,1110100011,1110100100,1110100101,1110100110,1110100111,1110101000,1110101001,1110101010,1110101011,1110101100,1110101101,1110101110,1110101111,1110110000,1110110001,1110110010,1110110011,1110110100,1110110101,1110110110,1110110111,1110111000,1110111001,1110111010,1110111011,1110111100,1110111101,1110111110,1110111111,1111000000,1111000001,1111000010,1111000011,1111000100,1111000101,1111000110,1111000111,1111001000,1111001001,1111001010,1111001011,1111001100,1111001101,1111001110,1111001111,1111010000,1111010001,1111010010,1111010011,1111010100,1111010101,1111010110,1111010111,1111011000,1111011001,1111011010,1111011011,1111011100,1111011101,1111011110,1111011111,1111100000,1111100001,1111100010,1111100011,1111100100,1111100101,1111100110,1111100111,1111101000,1111101001,1111101010,1111101011,1111101100,1111101101,1111101110,1111101111,1111110000,1111110001,1111110010,1111110011,1111110100,1111110101,1111110110,1111110111,1111111000,1111111001,1111111010,1111111011,1111111100,1111111101,1111111110,1111111111,10000000000,10000000001,10000000010,10000000011,10000000100,10000000101,10000000110,10000000111,10000001000,10000001001,10000001010,10000001011,10000001100,10000001101,10000001110,10000001111,10000010000,10000010001,10000010010,10000010011,10000010100,10000010101,10000010110,10000010111,10000011000,10000011001,10000011010,10000011011,10000011100,10000011101,10000011110,10000011111,10000100000,10000100001,10000100010,10000100011,10000100100,10000100101,10000100110,10000100111,10000101000,10000101001,10000101010,10000101011,10000101100,10000101101,10000101110,10000101111,10000110000,10000110001,10000110010,10000110011,10000110100,10000110101,10000110110,10000110111,10000111000,10000111001,10000111010,10000111011,10000111100,10000111101,10000111110,10000111111,10001000000,10001000001,10001000010,10001000011,10001000100,10001000101,10001000110,10001000111,10001001000,10001001001,10001001010,10001001011,10001001100,10001001101,10001001110,10001001111,10001010000,10001010001,10001010010,10001010011,10001010100,10001010101,10001010110,10001010111,10001011000,10001011001,10001011010,10001011011,10001011100,10001011101,10001011110,10001011111,10001100000,10001100001,10001100010,10001100011,10001100100,10001100101,10001100110,10001100111,10001101000,10001101001,10001101010,10001101011,10001101100,10001101101,10001101110,10001101111,10001110000,10001110001,10001110010,10001110011,10001110100,10001110101,10001110110,10001110111,10001111000,10001111001,10001111010,10001111011,10001111100,10001111101,10001111110,10001111111,10010000000,10010000001,10010000010,10010000011,10010000100,10010000101,10010000110,10010000111,10010001000,10010001001,10010001010,10010001011,10010001100,10010001101,10010001110,10010001111,10010010000,10010010001,10010010010,10010010011,10010010100,10010010101,10010010110,10010010111,10010011000,10010011001,10010011010,10010011011,10010011100,10010011101,10010011110,10010011111,10010100000,10010100001,10010100010,10010100011,10010100100,10010100101,10010100110,10010100111,10010101000,10010101001,10010101010,10010101011,10010101100,10010101101,10010101110,10010101111,10010110000,10010110001,10010110010,10010110011,10010110100,10010110101,10010110110,10010110111,10010111000,10010111001,10010111010,10010111011,10010111100,10010111101,10010111110,10010111111,10011000000,10011000001,10011000010,10011000011,10011000100,10011000101,10011000110,10011000111,10011001000,10011001001,10011001010,10011001011,10011001100,10011001101,10011001110,10011001111,10011010000,10011010001,10011010010,10011010011,10011010100,10011010101,10011010110,10011010111,10011011000,10011011001,10011011010,10011011011,10011011100,10011011101,10011011110,10011011111,10011100000,10011100001,10011100010,10011100011,10011100100,10011100101,10011100110,10011100111,10011101000,10011101001,10011101010,10011101011,10011101100,10011101101,10011101110,10011101111,10011110000,10011110001,10011110010,10011110011,10011110100,10011110101,10011110110,10011110111,10011111000,10011111001,10011111010,10011111011,10011111100,10011111101,10011111110,10011111111,10100000000,10100000001,10100000010,10100000011,10100000100,10100000101,10100000110,10100000111,10100001000,10100001001,10100001010,10100001011,10100001100,10100001101,10100001110,10100001111,10100010000,10100010001,10100010010,10100010011,10100010100,10100010101,10100010110,10100010111,10100011000,10100011001,10100011010,10100011011,10100011100,10100011101,10100011110,10100011111,10100100000,10100100001,10100100010,10100100011,10100100100,10100100101,10100100110,10100100111,10100101000,10100101001,10100101010,10100101011,10100101100,10100101101,10100101110,10100101111,10100110000,10100110001,10100110010,10100110011,10100110100,10100110101,10100110110,10100110111,10100111000,10100111001,10100111010,10100111011,10100111100,10100111101,10100111110,10100111111,10101000000,10101000001,10101000010,10101000011,10101000100,10101000101,10101000110,10101000111,10101001000,10101001001,10101001010,10101001011,10101001100,10101001101,10101001110,10101001111,10101010000,10101010001,10101010010,10101010011,10101010100,10101010101,10101010110,10101010111,10101011000,10101011001,10101011010,10101011011,10101011100,10101011101,10101011110,10101011111,10101100000,10101100001,10101100010,10101100011,10101100100,10101100101,10101100110,10101100111,10101101000,10101101001,10101101010,10101101011,10101101100,10101101101,10101101110,10101101111,10101110000,10101110001,10101110010,10101110011,10101110100,10101110101,10101110110,10101110111,10101111000,10101111001,10101111010,10101111011,10101111100,10101111101,10101111110,10101111111,10110000000,10110000001,10110000010,10110000011,10110000100,10110000101,10110000110,10110000111,10110001000,10110001001,10110001010,10110001011,10110001100,10110001101,10110001110,10110001111,10110010000,10110010001,10110010010,10110010011,10110010100,10110010101,10110010110,10110010111,10110011000,10110011001,10110011010,10110011011,10110011100,10110011101,10110011110,10110011111,10110100000,10110100001,10110100010,10110100011,10110100100,10110100101,10110100110,10110100111,10110101000,10110101001,10110101010,10110101011,10110101100,10110101101,10110101110,10110101111,10110110000,10110110001,10110110010,10110110011,10110110100,10110110101,10110110110,10110110111,10110111000,10110111001,10110111010,10110111011,10110111100,10110111101,10110111110,10110111111,10111000000,10111000001,10111000010,10111000011,10111000100,10111000101,10111000110,10111000111,10111001000,10111001001,10111001010,10111001011,10111001100,10111001101,10111001110,10111001111,10111010000,10111010001,10111010010,10111010011,10111010100,10111010101,10111010110,10111010111,10111011000,10111011001,10111011010,10111011011,10111011100,10111011101,10111011110,10111011111,10111100000,10111100001,10111100010,10111100011,10111100100,10111100101,10111100110,10111100111,10111101000,10111101001,10111101010,10111101011,10111101100,10111101101,10111101110,10111101111,10111110000,10111110001,10111110010,10111110011,10111110100,10111110101,10111110110,10111110111,10111111000,10111111001,10111111010,10111111011,10111111100,10111111101,10111111110,10111111111,11000000000,11000000001,11000000010,11000000011,11000000100,11000000101,11000000110,11000000111,11000001000,11000001001,11000001010,11000001011,11000001100,11000001101,11000001110,11000001111,11000010000,11000010001,11000010010,11000010011,11000010100,11000010101,11000010110,11000010111,11000011000,11000011001,11000011010,11000011011,11000011100,11000011101,11000011110,11000011111,11000100000,11000100001,11000100010,11000100011,11000100100,11000100101,11000100110,11000100111,11000101000,11000101001,11000101010,11000101011,11000101100,11000101101,11000101110,11000101111,11000110000,11000110001,11000110010,11000110011,11000110100,11000110101,11000110110,11000110111,11000111000,11000111001,11000111010,11000111011,11000111100,11000111101,11000111110,11000111111,11001000000,11001000001,11001000010,11001000011,11001000100,11001000101,11001000110,11001000111,11001001000,11001001001,11001001010,11001001011,11001001100,11001001101,11001001110,11001001111,11001010000,11001010001,11001010010,11001010011,11001010100,11001010101,11001010110,11001010111,11001011000,11001011001,11001011010,11001011011,11001011100,11001011101,11001011110,11001011111,11001100000,11001100001,11001100010,11001100011,11001100100,11001100101,11001100110,11001100111,11001101000,11001101001,11001101010,11001101011,11001101100,11001101101,11001101110,11001101111,11001110000,11001110001,11001110010,11001110011,11001110100,11001110101,11001110110,11001110111,11001111000,11001111001,11001111010,11001111011,11001111100,11001111101,11001111110,11001111111,11010000000,11010000001,11010000010,11010000011,11010000100,11010000101,11010000110,11010000111,11010001000,11010001001,11010001010,11010001011,11010001100,11010001101,11010001110,11010001111,11010010000,11010010001,11010010010,11010010011,11010010100,11010010101,11010010110,11010010111,11010011000,11010011001,11010011010,11010011011,11010011100,11010011101,11010011110,11010011111,11010100000,11010100001,11010100010,11010100011,11010100100,11010100101,11010100110,11010100111,11010101000,11010101001,11010101010,11010101011,11010101100,11010101101,11010101110,11010101111,11010110000,11010110001,11010110010,11010110011,11010110100,11010110101,11010110110,11010110111,11010111000,11010111001,11010111010,11010111011,11010111100,11010111101,11010111110,11010111111,11011000000,11011000001,11011000010,11011000011,11011000100,11011000101,11011000110,11011000111,11011001000,11011001001,11011001010,11011001011,11011001100,11011001101,11011001110,11011001111,11011010000,11011010001,11011010010,11011010011,11011010100,11011010101,11011010110,11011010111,11011011000,11011011001,11011011010,11011011011,11011011100,11011011101,11011011110,11011011111,11011100000,11011100001,11011100010,11011100011,11011100100,11011100101,11011100110,11011100111,11011101000,11011101001,11011101010,11011101011,11011101100,11011101101,11011101110,11011101111,11011110000,11011110001,11011110010,11011110011,11011110100,11011110101,11011110110,11011110111,11011111000,11011111001,11011111010,11011111011,11011111100,11011111101,11011111110,11011111111,11100000000,11100000001,11100000010,11100000011,11100000100,11100000101,11100000110,11100000111,11100001000,11100001001,11100001010,11100001011,11100001100,11100001101,11100001110,11100001111,11100010000,11100010001,11100010010,11100010011,11100010100,11100010101,11100010110,11100010111,11100011000,11100011001,11100011010,11100011011,11100011100,11100011101,11100011110,11100011111,11100100000,11100100001,11100100010,11100100011,11100100100,11100100101,11100100110,11100100111,11100101000,11100101001,11100101010,11100101011,11100101100,11100101101,11100101110,11100101111,11100110000,11100110001,11100110010,11100110011,11100110100,11100110101,11100110110,11100110111,11100111000,11100111001,11100111010,11100111011,11100111100,11100111101,11100111110,11100111111,11101000000,11101000001,11101000010,11101000011,11101000100,11101000101,11101000110,11101000111,11101001000,11101001001,11101001010,11101001011,11101001100,11101001101,11101001110,11101001111,11101010000,11101010001,11101010010,11101010011,11101010100,11101010101,11101010110,11101010111,11101011000,11101011001,11101011010,11101011011,11101011100,11101011101,11101011110,11101011111,11101100000,11101100001,11101100010,11101100011,11101100100,11101100101,11101100110,11101100111,11101101000,11101101001,11101101010,11101101011,11101101100,11101101101,11101101110,11101101111,11101110000,11101110001,11101110010,11101110011,11101110100,11101110101,11101110110,11101110111,11101111000,11101111001,11101111010,11101111011,11101111100,11101111101,11101111110,11101111111,11110000000,11110000001,11110000010,11110000011,11110000100,11110000101,11110000110,11110000111,11110001000,11110001001,11110001010,11110001011,11110001100,11110001101,11110001110,11110001111,11110010000,11110010001,11110010010,11110010011,11110010100,11110010101,11110010110,11110010111,11110011000,11110011001,11110011010,11110011011,11110011100,11110011101,11110011110,11110011111,11110100000,11110100001,11110100010,11110100011,11110100100,11110100101,11110100110,11110100111,11110101000,11110101001,11110101010,11110101011,11110101100,11110101101,11110101110,11110101111,11110110000,11110110001,11110110010,11110110011,11110110100,11110110101,11110110110,11110110111,11110111000,11110111001,11110111010,11110111011,11110111100,11110111101,11110111110,11110111111,11111000000,11111000001,11111000010,11111000011,11111000100,11111000101,11111000110,11111000111,11111001000,11111001001,11111001010,11111001011,11111001100,11111001101,11111001110,11111001111,11111010000,11111010001,11111010010,11111010011,11111010100,11111010101,11111010110,11111010111,11111011000,11111011001,11111011010,11111011011,11111011100,11111011101,11111011110,11111011111,11111100000,11111100001,11111100010,11111100011,11111100100,11111100101,11111100110,11111100111,11111101000,11111101001,11111101010,11111101011,11111101100,11111101101,11111101110,11111101111,11111110000,11111110001,11111110010,11111110011,11111110100,11111110101,11111110110,11111110111,11111111000,11111111001,11111111010,11111111011,11111111100,11111111101,11111111110,11111111111};
ll n;
bool p=0;

int main()
{
    int i;
    while(~scanf("%lld",&n))
    {
        if(!n) return 0;
        if(n==72)
        {
            printf("%lld\n",111111111000);
        }
        else if(n==99)
        {
            printf("%lld\n",111111111111111111);
        }
        else if(n==108)
        {
            printf("%lld\n",110111111100);
        }
        else if(n==144)
        {
            printf("%lld\n",1111111110000);
        }
        else if(n==189)
        {
            printf("%lld\n",1010111010111);
        }
        else if(n==198)
        {
            printf("%lld\n",1111111111111111110);
        }
        else for(i=1;;i++)
        {
            if(num[i]%n==0)
            {
                printf("%lld\n",num[i]);
                break;
            }
        }
    }
}

法2 DFS

经过估计答案一定在unsigned内,故可以确定数字长度最大值后进行深搜.
代码较短,但无法面对大数据

代码

#include<cstdio>
#define ll unsigned long long
using namespace std;

ll n;
bool have;

void dfs(ll now,ll len)
{
    if(have||len==19) return;
    if(now%n==0)
    {
        printf("%llu\n",now);
        have=1;
        return;
    }
    dfs(now*10,len+1);
    dfs(now*10+1,len+1);
}

int main()
{
    while(~scanf("%llu",&n))
    {
        if(!n) return 0;
        have=0;
        dfs(1,0);
    }
}

法3 BFS

一位一位枚举,每一种余数枚举到后就入队,下次枚举到后不再记录下来,最后余数为0时倒着输出.
虽然代码较长,但可以求大数据,而且能直接求出最小值,更加实用.

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

queue<int>que;
int used[210],n,pri[210],tmp,aa,ans[110];
bool have;

int main()
{
    int p,i;
    while(~scanf("%d",&n))
    {
        if(!n)
        {
            return 0;
        }
        if(n==1)
        {
            printf("1\n");
            continue;
        }
        while(!que.empty()) que.pop();
        memset(used,-1,sizeof(used));
        used[1]=1;
        que.push(1);
        pri[1]=-1;
        have=1;
        while(!que.empty()&&have)
        {
            tmp=que.front()*10;
            que.pop();
            if(used[tmp%n]==-1)
            {
                used[tmp%n]=0;
                pri[tmp%n]=tmp/10;
                que.push(tmp%n);
            }
            tmp++;
            if(used[tmp%n]==-1)
            {
                used[tmp%n]=1;
                pri[tmp%n]=tmp/10;
                que.push(tmp%n);
            }
            if(used[0]!=-1)
            {
                aa=p=0;
                while(p!=-1)
                {
                    aa++;
                    ans[aa]=used[p];
                    p=pri[p];
                }
                for(i=aa;i>=1;i--)
                {
                    printf("%d",ans[i]);
                }
                printf("\n");
                have=0;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值