zoukankan      html  css  js  c++  java
  • 【原】 POJ 1426 Find The Multiple BFS搜索 解题报告

    http://poj.org/problem?id=1426


    方法:

    找到n的倍数m,m中只能有0或1
    0,1可构造出二叉树进行BFS搜索
    该题的重点就在于如何构造出BFS进行搜索。
    所有0,1组成的数可以构造出一颗二叉树从而进行BFS。每层的数字长度相同,每个节点的左右孩子为其末尾分别添加0,1
    BFS搜索一颗二叉树。二叉树的节点按层序遍历依次为(1),(10,11),(100,101,110,111)....
    即每个节点vertex的邻接点为10*vertex和10*vertex+1

                   1
                  /  \
                 /    \
                /      \
              10        11
              / \       /\
             /   \     /  \
           100  101  110  111

    注意:
    1、使用STL的queue会TLE,改用自己手写的才126MS左右,差别很大
    2、自己手写的queue的capacity是测试出来的,即使得200以内的答案正确即可
    3、虽然题目中说m可能有200位那么长,似乎要用到bignum,但是经测试可知__int64足够了。

    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

       1: void run1426_2()
       2: {
       3:     int n ;
       4:     __int64 m ;
       5:     const int capacity = 9999999 ;
       6:     __int64 *myQueue = new __int64[capacity] ; 
       7:     int front,rear ;
       8:  
       9:     //for( n=1 ;n<=200; ++n )
      10:     while( scanf("%d",&n) && n!=0 )
      11:     {
      12:         rear = 0 ;
      13:         front = 1 ;
      14:  
      15:         myQueue[++rear] = 1 ;
      16:         while( true )
      17:         {
      18:             m = myQueue[front] ;
      19:             if(++front == capacity)
      20:                 front = 0 ;
      21:  
      22:             if( m%n==0 )
      23:                 break ;
      24:  
      25:             m = 10*m ;
      26:             if( m%n==0 )
      27:                 break ;
      28:             if(++rear == capacity)
      29:                 rear = 0 ;
      30:             myQueue[rear]=m ;
      31:  
      32:             m += 1 ;
      33:             if( m%n==0 )
      34:                 break ;
      35:             if(++rear == capacity)
      36:                 rear = 0 ;
      37:             myQueue[rear]=m ;
      38:         }
      39:         //printf("%d\t%d\n",n,m) ;
      40:         printf("%I64d\n",m) ;
      41:     }
      42:     delete []myQueue ;
      43: }

  • 相关阅读:
    filter函数示例
    组件里v-for示例
    操作数组的函数简介
    class绑定对象改进版
    python 全栈开发,Day6(函数进阶)
    python 全栈开发,Day5(函数初识)
    python 全栈开发,Day4(文件操作)
    python 全栈开发,Day3(集合,深浅copy)
    python 全栈开发,Day2(基础数据类型)
    python 全栈开发,Day1(python介绍,变量,if,while)
  • 原文地址:https://www.cnblogs.com/allensun/p/1870078.html
Copyright © 2011-2022 走看看