素数的判断

1既不是素数,也不是合数

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#include <sstream>
#include <cmath>
#include <vector>
#include <algorithm>


using namespace std;


bool is_prime(int n)
{

    if(n<=1)            //特例 
    {
        return false;
    }

    int sqr = (int)sqrt(1.0*n);  //1.0是为了将n扩展为double类型 


    for(int i=2;i<=sqr;i++)
    {
        if(n%i==0)
        {
            return false;
        }

    }


    return true;


}




int main()
{
    int n=7;

    bool it;

    it = is_prime(n);

    cout<<it<<endl; 


    return 0;
} 















全部评论

相关推荐

09-17 19:25
已编辑
太原理工大学 游戏测试
叁六玖:公司名发我,我要这个HR带我打瓦
我的秋招日记
点赞 评论 收藏
分享
09-04 00:41
中山大学 C++
鼠鼠能上岸吗:进行中是秋招大项目进行中,你还可以选别的岗位;已结束是这个岗位流程结束了;筛选中就是在简历筛选环节没hr捞
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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