AOJ 0009 Prime Number(求素数)

题意:给定一个数n,判断从2—n中的素数个数是多少。

KEY:这里有两种做法,一是没注释那个代码测试极端数据999999就会错的,二是注释掉的那段代码才能AC。对于一眼看上去很简单的题目,特别要注意时间复杂度。

#include <iostream>
#include <stdio.h>
#include <string.h>
const int maxn = 999999 + 5;
int num[maxn];

using namespace std;

int main()
{
    int t;
    int n, m;
    while(scanf("%d", &t) != EOF){
        int m = 0;
        memset(num, 0, sizeof(num));
        n = m = 0;
       for(int i = 3; i <= t; i++){
            n = 0;
            for(int j = 2; j < i; j++){
               if(i % j != 0)
                    n++;
                else
                    break;
            }
        if(n == (i - 2))
           m++;
      //  }
        //for(int i = 0; i <= t; i++){
         //   num[i] = i;
      //  }
       // for(int i = 2; i <= t; i++){
       //     if(num[i] != 0){
      //          //num[i] = 0;
       //         for(int j = 2; j * i <= t; j++){
      //              num[j * i] = 0;
      //          }
      //      }
      //  }
      //  for(int i = 2; i <= t; i++){
      //      if(num[i] != 0)
      //          m++;
      //  }
    }
    printf("%d\n", m + 1);
   // printf("%d\n", m);
    }
    return 0;
}
时间: 2024-11-18 18:51:17

AOJ 0009 Prime Number(求素数)的相关文章

AOJ - 0009 Prime Number (素数筛法) &amp;&amp; AOJ - 0005 (求最大公约数和最小公倍数)

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34870 求n内的素数个数. 1 /* *********************************************** 2 Author : zch 3 Created Time :2015/5/19 8:46:16 4 File Name :a.cpp 5 ************************************************ */

AOJ 0009 Prime Number

题意:给出n,求不大于n的素数有多少个. 算法:先用线性时间复杂度的筛法打素数表,对于每个输入统计不超过的素数个数. #include <cstdio> int p[100010]; bool np[1000010]; int cntp; void SievePrime(int n) { for (int i = 0; i <= n; ++i) np[i] = true; np[0] = false, np[1] = false; for (int i = 2; i <= n; +

JD 题目1040:Prime Number (筛法求素数)

OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下的数中选择最小的数是素数,然后去掉它的倍数. 依次类推.直到筛子为空时结束. 如有: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 1不是素数.去掉.剩下的数中2最小,是素数,去掉2的

POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】

Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Accepted: 10619 Description Some positive integers can be represented by a sum of one or more consecutive prime numbers. How many such representations d

POJ 2689 - Prime Distance - [筛法求素数]

题目链接:http://poj.org/problem?id=2689 Time Limit: 1000MS Memory Limit: 65536K Description The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousan

hdu2136Largest prime factor (关建在求素数,有点意思的题)

Problem Description Everybody knows any number can be combined by the prime number. Now, your task is telling me what position of the largest prime factor. The position of prime 2 is 1, prime 3 is 2, and prime 5 is 3, etc. Specially, LPF(1) = 0. Inpu

HDU2136_Largest prime factor【水题】【筛法求素数】

Largest prime factor Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7216    Accepted Submission(s): 2559 Problem Description Everybody knows any number can be combined by the prime number. Now

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);