zoukankan      html  css  js  c++  java
  • POJ-1426.Findthemultiple.(BFS)

    一开始模拟了一波大数取余结果超时了,最后改成long long过了emmm...  

      本题大意:给出一个200以内的数n,让你找出一个m使得m % n == 0,要求m只有1和0组成。

      本题思路:BFS模拟即可。

      参考代码:

     1 #include <cstdio>
     2 #include <queue>
     3 using namespace std;
     4 
     5 int mod;
     6 typedef long long LL;
     7 
     8 LL bfs() {
     9     LL now = 1;
    10     queue <LL> s;
    11     s.push(now);
    12     while(!s.empty()) {
    13         LL cur = s.front();
    14         s.pop();
    15         for(int i = 0; i < 2; i ++) {
    16             LL next;
    17             if(i == 1) {
    18                 next = cur * 10 + 1;
    19             } else next = cur * 10;
    20             if(next % mod == 0)  return next;
    21             s.push(next);
    22         }
    23     }
    24     return 0;
    25 }
    26 
    27 int main () {
    28     while(~scanf("%d", &mod) && mod) {
    29         LL ans = bfs();
    30         printf("%lld
    ", ans);
    31     }
    32     return 0;
    33 }
    View Code

      顺便给出模拟大数取余的代码。

     1 #include <string>
     2 #include <iostream>
     3 using namespace std;
     4 
     5 int main () {
     6     int mod;
     7     string s;
     8     while(cin >> mod >> s) {
     9         int ans = 0;
    10         for(int i = 0; i < s.length(); i ++)
    11             ans = (ans * 10 + s[i] - '0') % mod;
    12         cout << ans << endl;
    13     }
    14     return 0;
    15 }
  • 相关阅读:
    java学习笔记----数据类型,变量,常量
    java学习笔记 --- java基础语法
    java学习笔记----java入门
    python 类方法
    nginx 限制ip/限制访问路径
    python 异常处理
    python 内置模块
    python 模块与包
    python函数基础用法
    python函数高级运用
  • 原文地址:https://www.cnblogs.com/bianjunting/p/10482981.html
Copyright © 2011-2022 走看看