前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >求第K个素数----08年上海交大复试上机题第一题

求第K个素数----08年上海交大复试上机题第一题

作者头像
ccf19881030
发布2019-04-24 15:02:42
9040
发布2019-04-24 15:02:42
举报
文章被收录于专栏:ccf19881030的博客

08年上海交大复试上机题第一题

原题如下:

Problem A. Prime Number

Input file: Standard Input

Output file: Standard Output

Time Limit: 1 Second

Output the k-th prime number.

Input

k≤10000

Output

The k-th prime number.

Sample input and output

Standard Input

Standard Output 

代码语言:javascript
复制
#include<stdio.h>
#include<math.h>
#include<time.h>
#define MAX 10001
/**********************判断一个整数是否为素数**************************/ 
bool Is_Primer(int n)
{   if(n <= 1)
       return false;
	for(int i = 2; i <= sqrt(n); i++)
	{
		if(n % i == 0)
		    return false;
       else
         continue;
          
	}
	
	return true;
}

int main()
{
	double start,finish; /* 开始时间,结束时间 */
	
	int k,a[MAX] = {0};
	scanf("%d",&k);
	a[0] = 2;
	a[1] = 3;
	start = (double) clock();
/*******找到第k个素数,将1到10000个素数存到数组a[MAX]中************/ 
	for(int n =5,j = 2; j <10001; n += 2){ 
	  if(Is_Primer(n))
	   {
	     
	     a[j++] = n;
	   }
    
    }
	
	printf("The %d Primer number is %d/n",k,a[k-1]);
        
        finish = (double) clock();
        printf("The time was: %.4fs/n",(finish - start)/CLOCKS_PER_SEC);
        return 0;
    
	    	  
 	   
    
    
}

我的做法是写一个检验一个正整数是否为素数的函数

,然后在主函数中将1到10000个素数存放在数组a[10001]当中,

输出要找的第K个素数a[k-1]即可。

当然这道题对于经常编Acm题目的人来说是小菜一碟了,

它是08年上海交大复试上机题第一题。

另外为了测试程序的运行时间,

我用了C/C++中time.h中的clock函数

clock函数原型如下:

函数名: clock 功 能: 确定处理器时间 用 法: clock_t clock(void);

做出来题目的成就感真是太好了,但是我感觉自己的编程实际能力

实在是太差了,好多Acm题目都不会,看来得好好系统地学一下算法才行!

3

5

7

17

我写的程序:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2010年09月05日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档