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: