题解 | 素数判断

素数判断

https://www.nowcoder.com/practice/5ab1b9690af047699e96c87dee65def4

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

bool is_Prime(int x){
	if(x==0||x==1) return false;
	for(int i=2;i*i<=x;i++){ //i<x也行但程序运行速度慢 ,记住简化条件:i*i<=x
		if(x%i==0){
			return false;
		}
	}
	return true;
}

int main(){
	
	int t,n;
	cin>>t;
	
	for(int i=1;i<=t;i++){
		cin>>n;
		if(is_Prime(n)==true){
			cout<<"Yes"<<endl;
		}else{
			cout<<"No"<<endl;
		}
	}

    return 0;
}


全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务