题目大意:给你一个不超过200的n,要你找到一个只含有0和1的m能够整除n。
#include <iostream>
#include<cstdio>
#include<queue>
using namespace std;
void bfs(int n)
{
queue<long long>q;
q.push(1);
while(!q.empty())//如果队空则返回真
{
int i;
long long x;
x=q.front();//返回第一个元素
q.pop();//删除第一个元素
if(x%n==0)
{
printf("%lld\n",x);
return;
}
q.push(x*10);//在末尾加入一个元素
q.push(x*10+1);
}
}
int main(void)
{
int n;
while(scanf("%d",&n)&&n!=0)
{
bfs(n);
}
// cout << "Hello world!" << endl;
return 0;
}
C++队列queue模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
C++队列Queue是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
C++队列Queue类成员函数如下:
back()
返回最后一个元素
empty()
如果队列空则返回真
front()
返回第一个元素
pop()
删除第一个元素
push()
在末尾加入一个元素
size()
返回队列中元素的个数