zoukankan      html  css  js  c++  java
  • 九度OJ 1163 素数

    题目地址:http://ac.jobdu.com/problem.php?pid=1163

    题目描述:

    输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数,如果没有则输出-1。

    输入:

    输入有多组数据。
    每组一行,输入n。

    输出:

    输出所有从1到这个整数之间(不包括1和这个整数)个位为1的素数(素数之间用空格隔开,最后一个素数后面没有空格),如果没有则输出-1。

    样例输入:
    100
    样例输出:
    11 31 41 61 71
    来源:
    2008年北京航空航天大学计算机研究生机试真题
    #include <stdio.h>
    #include <math.h>
      
    int IsPrime (int n){
        if (n <= 1)
            return 0;
        int sq = (int)sqrt((double)n);
        while (sq >= 2){
            if (n % sq == 0)
                break;
            --sq;
        }
        return (sq >= 2) ? 0 : 1;
    }
      
    void Initialize(int Prime[], int n){
        int index = 1;
        int num = 3;
      
        Prime[0] = 2;
        while (index < n){
            if (IsPrime (num)){
                Prime[index] = num;
                ++index;
            }
            num += 2;
        }
    }
      
    int main(void){
        int k;
        int Prime[1500];
        int flag;
        int start;
        int end;
     
        Initialize(Prime, 1500);
        while (scanf ("%d", &k) != EOF){
            end = 0;
            start = 4;
            while (Prime[start] < k){
                if (Prime[start] % 10 == 1)
                    end = start;
                ++start;
            }
            start = 4;
            flag = (end >= start) ? 1 : 0;
            if (flag == 1){
                while (start < end){
                    if (Prime[start] % 10 == 1)
                        printf ("%d ", Prime[start]);
                    ++start;
                }
                printf ("%d
    ", Prime[start]);
            }
            else{
                printf ("-1
    ");
            }
        }
      
        return 0;
    }

    参考资料:维基百科 -- 素数

  • 相关阅读:
    Manacher-模版题poj3974 hdu3068
    拓展kmp(带注释版)
    颓の第17周
    php 递归遍历目录带缩进
    php 递归遍历目录
    php session
    apache主机配置
    php环境配置的检测方法
    php 变量
    php MVC
  • 原文地址:https://www.cnblogs.com/liushaobo/p/4373788.html
Copyright © 2011-2022 走看看