Find The Multiple——广搜+打表

陆佃

关注

阅读 31

2022-01-31

标题 Find The Multiple

在这里插入图片描述

分析

因为要求十进制数字中只能由0 1,这个数字从1 10 11 110 111…可看出来,均可从前一个数字*10 or *10+1得出;
那么就通过广度搜索遍历得出数字,然后再判断是否符合整除条件即可。
为了避免超时,采用打表方式进行先行计算。
坑:
最开始使用c++的编译环境,

代码

#include<iostream>
#include<queue>
using namespace std;
int n;
long long a[205];
void bfs() {
	queue<long long> Q;
	for(int i=1; i<=201; i++) {
		Q.push(1);
		while(!Q.empty()) {
			long long b=Q.front();
			if(b%i==0) {
				a[i]=b;
				break;
			}
			Q.pop();
			Q.push(b*10);//+0
			Q.push(b*10+1);//+1
		}
	}
}
int main() {
	bfs();
	while(cin>>n&&n) {
		cout<<a[n]<<endl;
	}
}

精彩评论(0)

0 0 举报