您的位置:首页 > 其它

zju/zoj 1136 Multiple(BFS)

2011-08-23 16:16 453 查看
就是不停的在后面加数。

#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cstdio>
#include<string>
using namespace std;
#define M 5002
bool use[M];

typedef struct li{
string path;
int num;
}li;
int main(){
int n,m,s[M],p;
li x,y;
while(scanf("%d %d",&n,&m)!=EOF){
for(int i=0;i<m;i++)
scanf("%d",&s[i]);
if(!n){
printf("0\n");
continue;
}
x.path="";
x.num=0;
sort(s,s+m);
memset(use,false,sizeof(use));
queue<li> q;
q.push(x);
while(!q.empty() && !use[0] ){
x=q.front();
q.pop();
for(int j=0;j<m && !use[0];j++){
y=x;
y.num=y.num*10+s[j];
if(!y.num)
continue;
p=y.num%n;

if(use[p])
continue;
y.num=p;
y.path+=(s[j]+'0');
use[p]=true;
q.push(y);
}
}
cout<<(use[0]?y.path:"0")<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: