Codeup 1946 issue B: Prime Number

Topic links : http://codeup.cn/problem.php?cid=100000591&pid=1

Title Description
Output the k-th prime number.

Enter
k≤10000

Output
The k-th prime number.

Sample input
10
50

Sample output of
29
229

Code

#include<stdio.h>

const int maxn = 110000; 		
int p[maxn] = {0};

int main() {
	int n, i;
	while(scanf("%d", &n) != EOF) {
		int num = 0;
		for(i = 2; i < maxn; i ++) {
			if(p[i] == 0) {
				num++;
				if(num == n)					
					break;
				for(int j = i + i; j < maxn; j += i) 
					p[j] = 1;
			}
		}
		printf("%d\n", i);
	}
	return 0;
}

[Note]: maxn not be too large, otherwise it will overrun time; nor too small, otherwise it will not find the first 10000.

Published 162 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/Rhao999/article/details/104199028