ZOJ 1312 题解这是一道简单题但容易出错,不需要使用任和算法就可做出来,而且很快。我的AC 0ms

 1 #include<cstdio>
2 #include<iostream>
3 #include<cmath>
4
5 using namespace std;
6
7 int Prime(int x)
8 {
9 int i=2,flag = 1;
10 while(i<=sqrt(double(x)) && flag==1)
11 {
12 if(x%i==0)
13 {
14 flag = 0;
15 break;
16 }
17 i++;
18 }
19 return flag;
20 }
21
22 int main()
23 {
24 int N, C, i, j;
25 int a[500],count,k;
26
27 while (cin>>N>>C)
28 {
29 j = 2;
30 a[1] = 1;
31 count = 1;
32 for(i=2; i<=N; i++)
33 {
34 if(Prime(i))
35 {
36 a[j++] = i;
37 count++;
38 }
39 }
40 printf("%d %d:",N,C);
41 if(2*C>count || 2*C-1>count)
42 {
43 for(i=1; i<=count; i++)
44 {
45 printf(" %d",a[i]);
46 }
47 }
48 else
49 {
50 if(count%2 == 0)
51 {
52 k = count/2;
53 for(i=k-C+1; i<=k+C; i++)
54 {
55 printf(" %d",a[i]);
56 }
57 }
58 else
59 {
60 k = count/2+1;
61 for(i=k-C+1; i<=k+C-1; i++)
62 {
63 printf(" %d",a[i]);
64 }
65 }
66 }
67 printf("\n\n");
68 }
69 return 0;
70 }
71
72

你可能感兴趣的:(ZOJ)