• 51Nod 1109 01组成的N的倍数


     

    准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
     收藏
     关注
    给定一个自然数N,找出一个M,使得M > 0且M是N的倍数,并且M的10进制表示只包含0或1。求最小的M。
     
    例如:N = 4,M = 100。
    Input
    输入1个数N。(1 <= N <= 10^6)
    Output
    输出符合条件的最小的M。
    Input示例
    4
    Output示例
    100
     
     
    找一个数,应该就是搜索 
    一开始 我写了priority_queue 但是他不断地乘 会爆long long (我并没有取模)
     
    一个数能整除n 等价于这个数%n==0 我们可以记录一下余数 若余数相同我们可以只入队一次
    记录m的话 只能有字符串了。。
     
     1 #include <queue>
     2 #include <cctype> 
     3 #include <cstdio>
     4 #include <iostream>
     5 
     6 using namespace std;
     7 
     8 typedef long long LL;
     9 const int MAXN=1000010;
    10 
    11 int n,t;
    12 
    13 struct node {
    14     string s;
    15     int x;
    16 };
    17 node a;
    18 
    19 std::queue<node> q;
    20 
    21 bool vis[MAXN];
    22 
    23 void BFS() {
    24     q.push(a);
    25     while(!q.empty()) {
    26         a=q.front();
    27         q.pop();
    28         if(a.x==0) {
    29             cout<<a.s<<endl;
    30             return;
    31         }
    32         node L=a;
    33         L.s+="0";
    34         L.x=L.x*10%n;
    35         if(!vis[L.x]) q.push(L),vis[L.x]=true;
    36         L=a;
    37         L.s+="1";
    38         L.x=(L.x*10+1)%n;
    39         if(!vis[L.x]) q.push(L),vis[L.x]=true;
    40     }
    41 }
    42 
    43 int hh() {
    44     scanf("%d",&n);
    45     a.s="1";a.x=1;
    46     vis[1]=true;
    47     BFS();
    48     return 0;
    49 }
    50 
    51 int sb=hh();
    52 int main(int argc,char**argv) {;}
    代码
     
  • 相关阅读:
    ex01 温度转换1
    12 字典的遍历在 Python2 与 Python3 中区别
    11 序列中有多个最值时会索引出哪个
    10 isalpha() 对于字母的定义
    09 Python3 的深拷贝与浅拷贝
    08 in
    07 len()
    06 “杠零”与空字符
    导航点击字体变色
    清除浮动
  • 原文地址:https://www.cnblogs.com/whistle13326/p/7608122.html
Copyright © 2020-2023  润新知