首页 > 其他 > 详细

<swustoj>?id=189 素数判定

时间:2016-07-30 13:27:05      阅读:585      评论:0      收藏:0      [点我收藏+]

链接http://acm.swust.edu.cn/problem/0189/

素数http://baike.baidu.com/view/10626.htm?fromtitle=%E7%B4%A0%E6%95%B0&fromid=115069&type=syn

在一般领域,对正整数n,如果用2到 技术分享 之间的所有整数去除,均无法整除,则n为质数

#include <stdio.h>
#include <math.h>
int main()
{
    void swap(int &a,int &b);
    bool primenumber(int num);
    int i;
    int count;
    int left,right;
    while(~scanf("%d %d",&left,&right))
    {
        count=0;
        swap(left,right);
        for(i=left;i<=right;i++)
        {
            if(primenumber(i))
            {
                count++;
            }
        }
        printf("%d\n",count);
    }
    return 0;
}
void swap(int &a,int &b)
{
    if(a>b)
    {
        int temp=a;
        a=b;
        b=temp;
    }
}
bool primenumber(int num)
{
    int i;
    for(i=2;i<=sqrt(num);i++)
    {
        if(num%i==0)
        {
            return false;
        }
    }
    return true;
}

 

<swustoj>?id=189 素数判定

原文:http://www.cnblogs.com/niconiconi/p/5720678.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!