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 #include<cmath>
 3 using namespace std;
 4 #define maxn 20000000
 5 bool f[maxn];
 6 int main() 
 7 {
 8     int n,ans=0;
 9     cin>>n;
10     for(int i=2;i<=n;i++) 
11     {
12         if(!f[i])
13         {
14             ans++;
15             for(int j=1;i*j<=20000000;j++)
16             f[i*j]=true;
17         }
18     }
19     cout<<ans;
20 }

 

相关文章:

  • 2022-02-14
  • 2021-12-23
  • 2021-12-27
  • 2022-02-14
  • 2021-12-16
  • 2021-08-02
  • 2022-12-23
猜你喜欢
  • 2021-08-27
  • 2021-12-11
  • 2021-08-31
  • 2021-08-31
  • 2021-06-08
相关资源
相似解决方案