Poj 1426--Find The Multiple(bfs或dfs)
2014-11-14 00:59
369 查看
Find The Multiple
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
Sample Output
题意:输入一个数n,然后输出m,其中m是n的倍数,且m中的每位必须是1或者0;
开始的时候读错题意了,这个地方可以思考一下,开始的时候我从1开始入栈,然后把1的10倍入栈,然后把1的10倍+1的数入栈,以后以次类推。
这样就可以把所有的m满足各位为0或1的数,存到栈里,每次入栈时,只要再满足此时的m是n的倍数,输出m,程序就可以结束了。
因为这个题说了,答案不唯一,只要输出满足要求的就可以了,输出满足条件的最小的那个m;
Time Limit: 1000MS | Memory Limit: 10000K | |||
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,然后输出m,其中m是n的倍数,且m中的每位必须是1或者0;
开始的时候读错题意了,这个地方可以思考一下,开始的时候我从1开始入栈,然后把1的10倍入栈,然后把1的10倍+1的数入栈,以后以次类推。
这样就可以把所有的m满足各位为0或1的数,存到栈里,每次入栈时,只要再满足此时的m是n的倍数,输出m,程序就可以结束了。
因为这个题说了,答案不唯一,只要输出满足要求的就可以了,输出满足条件的最小的那个m;
#include<iostream> #include<queue> using namespace std; int n; long long BFS() { long long t; queue<long long>q; while(!q.empty()) q.pop(); q.push(1); while(1) { t = q.front(); if(t%n==0) return t; q.pop(); q.push(t*10); q.push(t*10+1); } } int main() { while(cin>>n, n) { long long res = BFS(); cout<<res<<endl; } return 0; }
相关文章推荐
- POJ-1426-Find The Multiple(BFS DFS)
- POJ-1426-Find The Multiple (BFS +DFS)两种解法
- POJ1426 Find The Multiple (BFS | DFS)
- D - Find The Multiple POJ - 1426 ——DFS || BFS
- poj 1426 Find The Multiple (bfs / dfs)
- POJ 1426 Find The Multiple --- BFS || DFS
- DFS/BFS(同余模) POJ 1426 Find The Multiple
- poj 1426 Find The Multiple(DFS或打表)
- POJ 1426 Find The Multiple(BFS 水~)
- POJ训练计划1426_Find The Multiple(BFS)
- POJ 1426-Find The Multiple(bfs)
- Poj 1426 Find The Multiple 【DFS】
- POJ 1426 Find The Multiple(BFS 同余模定理)
- poj 1426 Find The Multiple (bfs 搜索)
- POJ 题目1426 Find The Multiple(DFS)
- POJ 1426 Find The Multiple (DFS)
- DFS-POJ-1426-Find The Multiple
- Find The Multiple (poj 1426 bfs)
- poj 1426 Find The Multiple special judge BFS or Doubi mode
- POJ 1426 Find The Multiple(同余模定理优化双入口BFS)