Obtain the k-th prime number -- the first question in the retest of Shanghai Jiao Tong University

Source: Internet
Author: User

Question 1 of Shanghai Jiao Tong University Review

The original question is as follows:

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

# Include <stdio. h> <br/> # include <math. h> <br/> # include <time. h> <br/> # define Max 10001 <br/>/*********************** judge an integer whether it is a prime number **********************/<br/> bool is_primer (int n) <br/> {If (n <= 1) <br/> return false; <br/> for (INT I = 2; I <= SQRT (N ); I ++) <br/>{< br/> If (N % I = 0) <br/> return false; <br/> else <br/> continue; </P> <p >}</P> <p> return true; <br/>}</P> <p> int main () <br/>{< br/> double start, finish;/* start time, end time */</P> <p> int K, A [Max] = {0 }; <br/> scanf ("% d", & K); <br/> A [0] = 2; <br/> A [1] = 3; <br/> Start = (double) Clock (); <br/>/******* find the K prime number, store one to 10000 prime numbers in array a [Max] **************/<br/> for (INT n = 5, j = 2; j <10001; n + = 2) {<br/> If (is_primer (n )) <br/>{</P> <p> A [J ++] = N; <br/>}</P> <p> printf ("the % d primer number is % d/N", K, A [k-1]); </P> <p> finish = (double) Clock (); <br/> printf ("the time was: %. 4fs/N ", (finish-Start)/clocks_per_sec); <br/> return 0; </P> <p>}

 

My approach is to write a function to check whether a positive integer is a prime number.

Then, in the main function, store one to 10000 prime numbers in array a [10001,

Output The K prime number to be searched a [k-1.

Of course, this question is a piece of cake for people who often compile ACM questions,

It was the first question in the Shanghai Jiao Tong University Review in.

 

In addition, to test the program running time,

I used the clock function in time. h in C/C ++.

The clock function prototype is as follows:

Function Name: Clock
Skill: Determine the processor time
Usage: clock_t clock (void );

 

It's a great sense of accomplishment, but I feel like I have the practical programming skills.

It's really bad. I don't know a lot about ACM. It seems that we have to systematically study the algorithm!

 

 

3

5

7

17

My programs:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.