POJ 1426 Find The Multiple
题目链接:POJ 1426
Description
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.
Input
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.
Output
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
题意
多组输入,输入一个数n,然后寻找到一个只包含0和1的十进制数,并且这个数可以被n整除,然后输出它
题解:
刚开始想复杂了,以为有什么巧妙的公式,后来发现这是完全的暴力题好吧??枚举从0,1,10,11开始依次类推的所有数,因为题目的长度限制,不用担心超时的问题,而且在找到的数不唯一时,输出任意一个就可以了。用bfs,从0,1开始,每次*10入队,*10+1入队,然后一直找下去,符合条件就输出。
代码
#include<iostream>
#include<queue>
using namespace std;
queue<long long>P;
int main() {
int n;
long long ans;
while(cin>>n,n){
while (!P.empty())
P.pop();
P.push(1);
long long t;
while (!P.empty()) {
t = P.front();
P.pop();
if (t%n == 0)
{
ans = t;
break;
}
P.push(t * 10);
P.push(t * 10 + 1);
}
cout << ans << endl;
}
return 0;
}