http://ac.jobdu.com/problem.php?pid=1040
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
importjava.util.Scanner;
publicclass
Main {
publicstatic
void
main(String[] args) {
Scanner
sc = newScanner(System.in);
while(sc.hasNext())
{
intn
= sc.nextInt();
System.out.println(getPrimeNumber(n));
}
}
publicstatic
int
getPrimeNumber(intn)
{
int[]
total = newint[10000];
total[0]
= 2;
intcount
= 1;
for(inti
= 3;
i <= 200000;
i++) {
booleanflag
= true;
inttemp
= i;
for(intj
= 2;
j <= Math.sqrt(temp); j++) {
if(temp
% j == 0)
{
flag
= false;
break;
}
}
if(flag)
{
if(count<10000)
total[count++]
= i;
}
}
if(n
> total.length)
return-1;
returntotal[n
- 1];
}
}
|