2022.7.23 AcWing

AcWing (每日一题感觉有点简单了。)

BFS + 试除法判定质数

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

int T;
LL x;
LL ans;

bool check(LL t) {
	for (LL i = 2; i <= t / i; i++) {
		if (t % i == 0)
			return false;
	}
	return true;
}

LL bfs(LL x) {
	queue<LL> q;
	q.push(x);
	while (!q.empty()) {
		LL t = q.front();
		q.pop();

		for (int i = (t == x ? 1 : 0); i <= 9; i++) {
			LL nt = t * 10 + i;
			if (check(nt))
				return nt;
			q.push(nt);
		}

	}

	return -1;
}

int main() {
	scanf("%d", &T);
	while (T--) {
		scanf("%lld", &x);
		ans = 0;

		ans = bfs(x);

		printf("%lld\n", ans);
	}


	return 0;
}

LeetCode:

TopSort + 构造
同时学习了如何运用多种容器建图,详细题解如下(LeetCode为什么不让我修改题解啊~):

https://leetcode.cn/problems/ur2n8P/solution/top-by-sen-xm-huap/

posted @ 2022-07-23 15:14  superPG  阅读(21)  评论(0编辑  收藏  举报