35.大质数

时间限制: 1 s

 空间限制: 1000 KB

 题目等级 : 黄金 Gold

题解

题目描述 Description

小明因为没做作业而被数学老师罚站,之后数学老师要他回家把第n个质数找出来。(1<=n<=100000)

老师随机写了几个数,交给了小明。小明百度找了很久,都没能解决。现在交给聪明的你。请你帮忙!

————————————————————————————————————————————

简单描述:把第n个质数找出来。

输入描述 Input Description

一个正整数n

1<=n<=100000)

输出描述 Output Description

n个质数。

(第1个质数为2,第2个质数为3。)

样例输入 Sample Input

样例1

2

样例2

65

样例3

20133

样例输出 Sample Output

样例1

3

样例2

313

样例3

226381

数据范围及提示 Data Size & Hint

有大数据等着你,小心超时,不许交表哦。

1<=n<=100000)

 

代码:

#include

#include

using namespace std;

#include

int main()

{

       int n;

       cin>>n;

       int t=0;

       for(long long i=2;;++i)

       {

              int flag=0;

              for(int j=2;j<=sqrt(i);++j)

              {

                     if(i%j==0)

                     {

                            flag++;

                            break;

                     }

              }

              if(flag==0) t++;

              if(t==n) {

                     printf("%lld",i);

                     return 0;

              }

       }

       return 0;

}

原文地址:https://www.cnblogs.com/c1299401227/p/5370788.html