首页 > 其他 > 详细

poj 1426

时间:2015-04-13 22:26:10      阅读:192      评论:0      收藏:0      [点我收藏+]

题意 : 给定一个n求只包括0与1的数 能被n整除 任意一个答案就可以

#include<iostream>
#include<stdio.h>
#include<cstring>
#include <queue> 
using namespace std; 
 
#define CLR(arr,val) memset(arr,val,sizeof(arr)) 
typedef long long LL; 
int flag[210]; 
LL bfs(int n){ 
    CLR(flag,0); 
    queue<LL> qq; 
    qq.push(1); 
    flag[1%n] = 1; 
    while(!qq.empty()){ 
        LL x = qq.front(); 
        if(x % n == 0) 
            return x; 
        qq.pop(); 
        LL y = x * 10; 
        int yy = y % n; 
        LL z = x * 10 + 1; 
        int zz = z % n; 
        if(!flag[yy]){ //这里为减枝  就是求最短的那个答案
           qq.push(y); 
           flag[yy] = 1; 
        } 
        if(!flag[zz]){ 
           qq.push(z); 
           flag[zz] = 1; 
        } 
    } 
} 
int main(){ 
    //freopen("1.txt","r",stdin); 
    int n; 
    while(scanf("%d",&n) &&n){ 
       LL ans = bfs(n); 
       printf("%lld\n",ans); 
    } 
    return 0; 
}

 

poj 1426

原文:http://www.cnblogs.com/zhangdashuai/p/4423150.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!