您的位置:首页 > 其它

Find The Multiple(bfs)

2013-10-21 13:21 330 查看

Find The Multiple

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 43 Accepted Submission(s) : 21
Special Judge
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
6

19

0

[align=left]Sample Output[/align]

10

100100100100100100

111111111111111111

[align=left]Source[/align]
PKU
题意:构造一个十进制由0和1组成的整数m,让m能够被n整除;题目中给出了m是小于等于100位的。

思路:bfs可以做出的,只是100位这个条件让我很头疼,我先用string试了试交了之后是超时,后来看了看答案用long long也给过了,所以m肯定没有100位的;

string类型代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;

queue<long long>s;
int a,c;
long long b;

int bfs()
{
while(!s.empty()){
b=s.front();
s.pop();
if(b%a==0){
cout<<b<<endl;
while(!s.empty()){
s.pop();
}
return 0;
}
else{
s.push(b*10);
s.push(b*10+1);
}
}
return 0;
}

int main()
{
//    freopen("input.txt","r",stdin);
while(cin>>a){
if(a==0)
break;
else{
s.push(1);
bfs();
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: