poj1426——Find The Multiple
来源:互联网 发布:网络电影扫黄完整版 编辑:程序博客网 时间:2024/05/16 07:57
题目大意:给出一个正整数n,找出它的倍数m,要求m的十进制形式只由0和1组成并且不超过100位数字
输入:(可以有很多个case,输入以0结尾)
第i个case的n(1<=n<=200)
输出:(如果有多个m,则输出任意一个)
第i个case的m
分析:bfs搜索,一位一位地确定,每位只有两种选择即0或1,每次扩展都在末尾加0或1,初始化最高位肯定为1,要注意到如果i为偶数,则不用计算,直接ans[i]=ans[i/2]+"0";
根据同余模定理,可以将前一步所得的余数当做前一步的字符串
代码:转载自http://blog.csdn.net/wangjian8006/article/details/7460523
- #include <iostream>
- #include <string>
- #include <queue>
- using namespace std;
- typedef struct{
- int mod;
- string ans;
- }Node;
- Node node[210],temp,now;
- bool dis[210];
- void bfs(int i){
- int x,y;
- queue <Node>q;
- memset(dis,false,sizeof(dis));
- dis[1]=true;
- temp.ans="1";
- temp.mod=1;
- q.push(temp);
- while(!q.empty()){
- now=temp=q.front();
- q.pop();
- x=(temp.mod*10+1)%i;
- y=(temp.mod*10)%i;
- if(!x){
- node[i].ans=temp.ans+"1";
- return ;
- }
- if(!y){
- node[i].ans=temp.ans+"0";
- return ;
- }
- if(!dis[x]){
- temp.ans=temp.ans+"1";
- temp.mod=x;
- q.push(temp);
- dis[x]=true;
- }
- if(!dis[y]){
- now.ans=now.ans+"0";
- now.mod=y;
- q.push(now);
- dis[y]=true;
- }
- }
- }
- int main(){
- int i,n;
- node[1].ans="1";
- for(i=2;i<=200;i++){
- if(i%2==0) node[i].ans=node[i/2].ans+"0";
- else bfs(i);
- }
- while(scanf("%d",&n) && n){
- cout<<node[n].ans<<endl;
- }
- return 0;
- }
阅读全文
0 0
- poj1426——Find The Multiple
- POJ1426——Find The Multiple
- POJ1426 Find The Multiple —— BFS
- poj1426——Find The Multiple
- POJ1426 Find The Multiple
- poj1426 Find The Multiple
- poj1426 - Find The Multiple
- POJ1426 Find The Multiple
- POJ1426-Find The Multiple
- poj1426 Find The Multiple
- poj1426-Find The Multiple
- POJ1426 Find the multiple
- POJ1426-Find The Multiple
- POJ1426 Find The Multiple
- poj1426 Find The Multiple
- Find The Multiple poj1426
- poj1426 find the multiple
- POJ1426 - Find The Multiple
- 获取Spring上下文即ApplicationContext
- HEVC代码学习31:xCheckRDCostMerge2Nx2N函数
- AD原理图再学笔记
- Vue.js解析(六)【Virtual DOM与diff(Vue.js实现)】
- 正则表达式中零宽断言(正则环视) grep -oP参数
- poj1426——Find The Multiple
- python selenium 如何切换到链接新打开的窗口
- Threapoolexecutor
- android轻量级数据库SQlite的工具类
- VMware怎么全屏
- python设计模式总结
- java中ArrayList源码方法理解--add(),remove()
- 私有云落地解决方案之网络篇-关键技术-快速重路由FRR
- mq操作相关指令