跳到正文

题解

题解:P5736 【深基7.例2】质数筛

starlit_tranquil约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年6月26日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 1 分钟
获赞
14
题目
P5736

版本

  1. v1当前

思路

用小于此数并大于一的整数去试除,如果又能够被它整除的,即为合数,否,即为质数。

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
int n;
int main() {
	cin >> n;
	for(int i=1;i<=n;i++){
		int b,pan=0;//pan 记录是否为合数
		cin>>b;
		for(int j=2;j<b;j++){
			if(b%j==0){
				pan=1;//是合数
			}
		}
		if(!pan&&b!=1)cout<<b<<' ';//特判'1'
	}
	return 0;
}
原文
存档评论6 条抓取于