51nod 1109 01组成的N的倍数 【dfs+剪枝+vector】

23 篇文章 0 订阅
14 篇文章 0 订阅

基准时间限制: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





代码:

#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
bool yu[1000100];
struct node{
    vector<char> ch;
    int shu;
}now,qian;
int main()
{
    int n;
    scanf("%d",&n);
    if(n==1)
        printf("1\n");
    else
    {
        queue<node> que;
        now.shu=1;
        now.ch.push_back('1');
        que.push(now);
        int a,b;yu[1]=true;
        bool fafe=false;
        while (true)
        {
            now=que.front();
            que.pop();
            a=now.shu*10;
            for (int i=0;i<2;i++)
            {
                b=(a+i)%n;
                if (b==0)
                {
                    for (int i=0;i<now.ch.size();i++)
                        printf("%c",now.ch[i]);
                    printf("%d\n",i);
                    fafe=true;
                    break;
                }
                if (yu[b]) continue;
                yu[b]=true;
                qian.ch=now.ch;
                qian.ch.push_back(i+48);
                qian.shu=b;
                que.push(qian);
            }
            if (fafe) break;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值