题目链接:http://poj.org/problem?id=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
Source
题意:给出一个整数n,(1 <= n <= 200)。求出任意一个它的倍数m,要求m必须只由十进制的’0’或’1’组成。m第一个数字必须是1,且n不大于200,m不超过100个十进制数字。
题解:搜索,队列都可以的,但是一定不要忘记将队列清空。
#include<iostream>//队列代码
#include<queue>
using namespace std;
#define ll long long
ll n;
queue<ll>q;
void bfs()
{
q.push(1);//从1开始
while(!q.empty()){
ll front=q.front();
if(front%n==0)
{
cout<<front<<endl;
return ;
}
q.pop();
for(int i=0;i<2;i++)
{
ll x=front*10+i;
if(x%n==0)
{
cout<<x<<endl;
return ;
}
q.push(x);
}
}
}
int main()
{
while(cin>>n&&n)
{
while(!q.empty()){
q.pop();}
bfs();
}
return 0;
}
#include <iostream>//搜索
#include <algorithm>
#include <cstdio>
using namespace std;
int n,m;
queue <long long> q;
void bfs()
{
q.push(1);
while(!q.empty())
{
long long t=q.front();
q.pop();
if(t%n==0)
{
cout<<t<<endl;
return ;
}
q.push(t*10);
q.push(t*10+1);
}
return ;
}
int main()
{
while(cin>>n&&n)
{
while(!q.empty())
{
q.pop();
}
bfs();
}
return 0;
}