首页 > 技术文章 > POJ - 1426(Find The Multiple)

20172674xi 2018-08-28 01:54 原文

Find The Multiple:

题目链接:

戳一戳

题目:

题意:

找出任意一个由0和1组成的数,而且是n的倍数。unsigned __int64可以存下结果,循环深度<20;

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
int n;
void bfs(int n)
{
    queue<long long>q;
    long long b,now,next;
    b=1;
    q.push(b);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int d=0; d<2; d++)
        {
            if(d==1)
            {
                next=now*10;
                if(next%n==0)
                {
                    printf("%lld\n",next);
                    return;
                }
            }
            else
            {
                next=(now*10)+1;
                if(next%n==0)
                {
                    printf("%lld\n",next);
                    return;
                }
            }
            q.push(next);
        }
    }
}
 
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        bfs(n);
    }
    return 0;
}

 

推荐阅读