1047.素数判定

Posted bernieloveslife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1047.素数判定相关的知识,希望对你有一定的参考价值。

题目描述:

给定一个数n,要求判断其是否为素数(0,1,负数都是非素数)。

输入:

测试数据有多组,每组输入一个数n。

输出:

对于每组输入,若是素数则输出yes,否则输入no。

样例输入:
13
样例输出:
yes

#include<iostream>
#include<cmath>
using namespace std;

bool judge(int x){
    if(x<=1) return false;
    for(int i=2;i<(int)sqrt(x)+1;i++){
        if(x%i==0) return false;
    }
    return true;
}

int main(){
    int n;
    while(cin>>n){
        if(judge(n)) cout<<"yes"<<endl;
        else cout<<"no"<<endl;
    }
    return 0;
}

 



以上是关于1047.素数判定的主要内容,如果未能解决你的问题,请参考以下文章