zoukankan      html  css  js  c++  java
  • Find the Multiple POJ1426

    题目链接:Find the Multiple

    题目大意

    找出一个只由0和1组成的能整除n的数。

    思路

    所有由0和1组成的数可以看作是某个只由0、1组成的数a经过以下两种变化得到

      1、a * 10

      2、a * 10+1

    因此只需要用BFS搜索满足条件的数即可。

    题解

     1 #include <iostream>
     2 #include <cstring>
     3 #include <queue>
     4 
     5 using namespace std;
     6 
     7 long long n;
     8 int main(int argc, char const *argv[])
     9 {
    10     while(cin >> n && n)
    11     {
    12         queue<long long> q;
    13         q.push(1);
    14         while(!q.empty())
    15         {
    16             if(q.front() % n == 0)
    17             {
    18                 cout << q.front() << endl;
    19                 break;
    20             }
    21             q.push(q.front()*10);
    22             q.push(q.front()*10+1);
    23             q.pop();
    24         }
    25     }
    26     return 0;
    27 }
  • 相关阅读:
    053467
    053466
    053465
    NC201613 Jelly
    NC14608 after与迷宫
    NC14572 走出迷宫
    340. 通信线路
    1135. 新年好
    903. 昂贵的聘礼
    P5767 [NOI1997]最优乘车
  • 原文地址:https://www.cnblogs.com/SaltyFishQF/p/10298984.html
Copyright © 2011-2022 走看看