ACM: 模拟队列判定 poj 1426

来源:互联网 发布:top k算法 java 编辑:程序博客网 时间:2024/05/12 13:11

                                                                  Find The Multiple

Description

Given a positive integer n, write a program to find out anonzero multiple m of n whose decimal representation contains onlythe digits 0 and 1. You may assume that n is not greater than 200and there is a corresponding m containing no more than 100 decimaldigits.

Input

The input file may contain multiple test cases. Each linecontains 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 thecorresponding value of m. The decimal representation of m must notcontain more than 100 digits. If there are multiple solutions for agiven value of n, any one of them is acceptable.

 

Sample Input

2
6
19
0

Sample Output

  10

  100100100100100100

  111111111111111111


题意: 找出十进制n 的 m倍的数,但是这个数必须有0 和 1组成.

解题思路:
                 1. 一开始看有点像数论. 但是仔细想,不用那么复杂.
                 2. 数据的规模不大,n不超过200, 队列广搜.
                 3. 模拟队列判定就OK了!!

代码:

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 2000005

int n;
long long a[MAX];

int main()
{
//   freopen("input.txt","r",stdin);
   while(scanf("%d",&n) != EOF&& n != 0)
    {
      int cur = 0;
      int total = 0;
      a[cur] = 1;
      
      while(true)
      {
         if( a[cur] % n == 0 )
         {
            printf("%lld\n",a[cur]);
            break;
         }
         else
         {
            a[++total] = a[cur] * 10;
            a[++total] = a[cur] * 10 + 1;
            cur++;
         }
      }
    }
    return0;
}

0 0