Poj Find The Multiple 需要二刷 *bfs,没遇到过的问题

基本思想:

还是卡在了建模上,个人觉得BFS一定要注意一下方面:

1.状态,一定要找一个可以拓展的状态,可以进行迭代;

2.边界,例如本题,如果使用广搜,就是取模为0,如果使用深搜,就是没有溢出,longlong最多为19位;

但是这个题目还有有一个疑点,就是内存超限问题;

看到两百位自己想到了大数类型的除法问题的,但是爆内存了,根据监控最高63MB,还是想不通是什么情况;

关键点:

无;

附上问题代码:

#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 101;

bool devide(string s, int num) {
	//检验是否能被整除
	int r = 0;
	for (int i = 0; i < s.size(); i++) {
		//对于大数大头在前面;
		int temp = (s[i] - '0') + r * 10;
		r = temp % num;
	}
	if (r == 0)
		return true;
	else
		return false;
}

string bfs(int n) {
	string s = "1";
	queue<string>q;
	q.push(s);
	while (!q.empty()) {
		string str = q.front();
		q.pop();
		for (int i = 0; i < 2; i++) {
			if (i == 0) {
				str += "0";
			}
			else {
				str += "1";
			}
			if (devide(str, n))
				return str;
			q.push(str);
		}
	}
}

int main() {
	int n;
	while (cin >> n) {
		if (n == 0)
			return 0;
		cout << bfs(n) << endl;
	}
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12444458.html