题目1040:Prime Number

来源:互联网 发布:php工程师个人介绍 编辑:程序博客网 时间:2024/04/28 16:03

http://ac.jobdu.com/problem.php?pid=1040

#include<stdio.h>
#include<math.h>
int a[200005];                                               
void initial()
{
   long int i;
   int j,count=2;
   a[1]=2; a[2]=3;
   for(i=4;i<200000;i++){ 
      for(j=2;j<=sqrt(i);j++){                      // 第10000个素数是104729,所以要注意求素数的方法,这里用的是sqrt(i)
          if(i%j==0) break;
      }
      if(j>sqrt(i)){
         count++;
         a[count]=i;
      } 
   }
}
int main()
{
    int i,n;
    initial();
    while(scanf("%d",&n)!=EOF){
        printf("%d\n",a[n]); 
    }
    system("pause");
    return 0;
}

原创粉丝点击