POJ 1426 Find The Multiple
来源:互联网 发布:杨焕明 知乎 编辑:程序博客网 时间:2024/05/19 04:02
题目链接~~>
做题感悟:做了杭电上的两个题目,再做这个真是 so easy !但是还要写一下的,用第二种方法很不熟练。。。
解题思路:还是用取余的思想,每次都将余数乘 10 再加 上相应的数,0 要特判一下。
代码1:
#include<stdio.h>#include<iostream>#include<string>#include<string.h>#include<queue>#include<algorithm>using namespace std ;int n ;bool vis[300] ; // 标记余数是否相同struct zhang{ int x,num ; string str ; // 这个很好用} ;int bfs(){ queue<zhang>q ; zhang curt,next ; memset(vis,false,sizeof(vis)) ; curt.x=0 ; curt.str="" ; curt.num=0 ; q.push(curt) ; while(!q.empty()) { curt=q.front() ; q.pop() ; if(curt.num>100) continue ; for(int i=0 ;i<2 ;i++) { next=curt ; next.num+=1 ; next.x=next.x*10+i ; if(!next.x) continue ; if(next.x%n==0) { next.str+=(48+i) ; next.str+='\0' ; cout<<next.str<<endl ; return true ; } next.x=next.x%n ; if(vis[next.x]) continue ; vis[next.x]=true ; next.str+=(48+i) ; q.push(next) ; } } return false ;}int main(){ while(scanf("%d",&n)!=EOF) { if(!n) break ; bfs() ; } return 0 ;}
代码2:
#include<stdio.h>#include<string.h>#include<queue>#include<iostream>#define MX 5000using namespace std ;int n ;bool vis[300] ;struct zhang{ int x,m,pre,num ;}q[MX];void print(int k) // 输出 不用string 存数{ if(k==-1) return ; print(q[k].pre) ; printf("%d",q[k].x) ;}int bfs(){ zhang curt,next ; int start=-1,head=0 ; memset(vis,false,sizeof(vis)) ; curt.x=1 ; curt.num=1 ; curt.pre=-1 ; if(n==1) { cout<<1<<endl ; return true ; } vis[1]=true ; curt.m=1 ; q[++start]=curt ; while(head<=start) { int h=head ;head++ ; curt=q[h] ; if(!curt.m) { print(h) ; printf("\n") ; return true ; } if(curt.num>100) continue ; for(int i=0 ;i<2 ;i++) { next=curt ; next.m=next.m*10+i ; if(!next.m) continue ; next.m=next.m%n ; if(vis[next.m]) continue ; vis[next.m]=true ; next.x=i ; next.num+=1 ; next.pre=h ; q[++start]=next ; } } return false ;}int main(){ while(scanf("%d",&n)!=EOF) { if(!n) break ; bfs() ; } return 0 ;}
0 0
- poj 1426 Find The Multiple
- Poj 1426 Find The Multiple
- POJ 1426 Find the Multiple
- poj 1426Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- poj - 1426 - Find The Multiple
- POJ 1426 Find The Multiple
- POJ-1426-Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- poj 1426 Find The Multiple
- POJ 1426 Find The Multiple
- # ## @#(字符串化操作符,粘接附,字符化操作符)
- C# ArrayList循环遍历ListBox
- android 微信朋友圈 中 回复功能的实现 是怎么弄的呢?寻求帮助
- XML解析之——使用dom4j解析XML
- 用xib文件自定义UIview的实现
- POJ 1426 Find The Multiple
- python模块学习---mechanize(模拟浏览器)
- Reactor模式,或者叫反应器模式
- 记
- 两个郁闷的异常: java.lang.StackOverflowError和java.net.SocketException: Broken pipe
- 【Intel DPDK】Qos_Sched实例在虚拟机中运行注意事项。
- 关于 横竖屏切换
- 背单词
- margin折叠的问题