Find The Multiple(bfs)

Description

Given a positive integer n,

给你正整数n

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

找出n的没有0的 非零倍数m ,    m的十进制表示只有01

You may assume that n is not greater than 200

n <= 200

and there is a corresponding m containing no more than 100 decimal digits.

对应m 不超过100位数字

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

思路

2 的1倍 = 2 是01组成吗?   2 的2倍 是01组成吗 。。。。很复杂。。

显然要逆向思考

num * 10  、 num * 10 +1 肯定是01 组成   == 末尾追加了0 或者 1

考虑所有由01组成的数,然后看这个数 是不是m的倍数 

#include <iostream>
#include <queue>
#include <cmath>
using namespace std;


queue<long long> q;

long long bfs(int n) {
	while(!q.empty()) {
		q.pop();
	}

	long long father;
	long long child;

	q.push(1);
	while(!q.empty()) {
		father = q.front();
		q.pop(); // 这个竟然忘了加!!多可恶啊!!

		if (father % n == 0) {
			return father;
		}

		q.push(father * 10);
		q.push(father * 10 + 1);

	}

}
int main() {
	int n;

	while(cin >> n) {
		long long res = bfs(n);
		printf("%lld\n",res);
	}

}
发布了86 篇原创文章 · 获赞 0 · 访问量 3632

猜你喜欢

转载自blog.csdn.net/bijingrui/article/details/104762713
今日推荐