POJ

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
",next);
                    return;
                }
            }
            else
            {
                next=(now*10)+1;
                if(next%n==0)
                {
                    printf("%lld
",next);
                    return;
                }
            }
            q.push(next);
        }
    }
}
 
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)break;
        bfs(n);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/20172674xi/p/9545742.html