zoukankan      html  css  js  c++  java
  • 素数

    素数1:

    #include<stdio.h>
    #include<math.h>
    int Judge(int n)
    {
        int i, k;
        if (n == 0 || n == 1)
            return 0;
        k = sqrt(n);
        for (i = 2; i <= k; i++)
        {
            if (n % i == 0)
                return 0;
        }
        return 1;
    }
    int main ()
    {
        int n;
        while (scanf("%d", &n) != EOF)
        {
            if (Judge(n))
                printf("Yes ");
            else
                printf("No ");
        }
    }

    素数2:(输入一个整数n(1<=n<=1000000)以EOF结束(大约有100w组数据))

    # include <stdio.h>
    # define N 1000010
    int a[N] = {1, 1};
    void prime(int a[])
    {
        int i, j;
        for (i = 2; i < N; i++)
        {
            if (a[i] == 0)
            {
                for (j = i + i; j < N; j+=i)
                    a[j] = 1;
            }
        }
    }
    int main ()
    {
        prime(a);
        int n;
        while (scanf("%d", &n) != EOF)
        {
            if (a[n] == 0)
                printf("Yes ");
            else
                printf("No ");
        }
        return 0;
    }
    素数3:(输入一个数N(1<=N<=10^12)以EOF结束(大约1W组数据))
    # include <stdio.h>
    # include <math.h>
    # define N 1000001
    int a[N] = {1, 1}, b[N], k; 
    void prime()
    {
        for(int i = 2; i < N; i++)
        {
            if(a[i] == 0)
            {
                b[k++] = i;
                for(int j = i + i; j < N; j += i)
                    a[j] = 1;
            }
        }

    int judge(long long n)
    {
        if(n == 1 || n == 0)
            return 0;   
        for(int i = 0; (long long)b[i] * b[i] <= n; i++)
            if(n % b[i] == 0)   //对b[i]取余
                return 0;
        return 1;
    }
    int main()
    {
        prime();
        long long n;
        while(scanf("%lld", &n) != EOF)
        {
            if(judge(n))
                printf("Yes ");
            else
                printf("No ");
        }
        return 0;
    }
  • 相关阅读:
    connot connect to mysql 10061
    <context:annotation-config/>,<mvc:annotation-driven/>和<context:component-scan>之间的关系
    Failed to read artifact descriptor for xxx:jar的问题解决
    URI is not registered ( Setting | Project Settings | Schemas and DTDs )
    MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
    springmvc.xml,context.xml和web.xml
    web.xml
    Spring配置文件XML详解
    springmvc配置文件
    Android设置窗口、控件透明度
  • 原文地址:https://www.cnblogs.com/syhandll/p/4437837.html
Copyright © 2011-2022 走看看