5790 素数序数(筛素数版)

5790 素数序数(筛素数版)

 

时间限制: 1 s
空间限制: 32000 KB
题目等级 : 黄金 Gold
题目描述 Description

给定一个整数n,保证n为正整数且在int范围内,输出n是第几个质数

建议使用筛素数法

输入描述 Input Description

一行,一个整数

输出描述 Output Description

一行,一个整数

样例输入 Sample Input

3

样例输出 Sample Output

2

数据范围及提示 Data Size & Hint

int就够了。。。

然后看限制和题目名称,你懂得

别的没啥了吧

 1 #include<iostream>
 2 using namespace std;
 3 bool sushu[210000000];
 4 int n,ans;
 5 int main()
 6 {
 7     sushu[1]=sushu[0]=1;
 8     cin>>n;
 9     for(int i=2;i<=sqrt(n);++i)
10     {
11         if(sushu[i]==0)for(int j=i*i;j<=(n);j+=i)
12         {
13             sushu[j]=1;
14         }
15     }
16     for(int i=1;i<=n;++i)
17     {
18         if(sushu[i]==0)
19         ans++;
20     }
21     cout<<ans;
22     return 0;
23  } 
原文地址:https://www.cnblogs.com/mjtcn/p/6740461.html