POJ - 1426(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;
}

猜你喜欢

转载自blog.csdn.net/WQN20172674/article/details/81355341