Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
3s | 8192K | 1346 | 447 | Standard |
A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.
21 2 18 2 18 18 100 7
21 2: 5 7 11 18 2: 3 5 7 11 18 18: 1 2 3 5 7 11 13 17 100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67
This problem is used for contest: 108
Submit / Problem List / Status / Discuss
主要是控制输出素数的个数和从第几个开始输出。
注意c大于某个数时即为全部输出。
代码写得略笨...有点复杂
1 #include <stdio.h> 2 3 int a[1005]; 4 5 int main() 6 { 7 int i, j, c, n, cnt, tmp, start; 8 9 for (i=2; i<40; ++i) 10 { 11 for (j=i; j*i<1005; ++j) 12 { 13 a[i*j] = 1; 14 } 15 } 16 17 while (scanf("%d%d", &n, &c) == 2) 18 { 19 cnt = 0; 20 for (i=1; i<=n; ++i) 21 { 22 if (!a[i]) 23 { 24 ++cnt; 25 } 26 } 27 28 tmp = 0; 29 printf("%d %d:", n, c); 30 int flag = 0; 31 if (c >= (cnt+1)/2) //输出全部素数的情况 32 { 33 for (i=1; i<=n; ++i) 34 { 35 if (!a[i]) 36 { 37 printf(" %d", i); 38 } 39 } 40 } 41 else 42 { 43 if (0 == cnt % 2) //总共偶数个素数 44 { 45 start = cnt/2-c+1; //从第start个开始输出 46 for (i=1; i<=n; ++i) 47 { 48 if (!a[i]) 49 { 50 ++tmp; 51 if (start == tmp) 52 { 53 flag = 1; 54 } 55 if (tmp == start+2*c) 56 { 57 break; 58 } 59 if (flag) 60 { 61 printf(" %d", i); 62 } 63 } 64 } 65 } 66 else 67 { 68 start = cnt/2-c+2; 69 for (i=1; i<=n; ++i) 70 { 71 if (!a[i]) 72 { 73 ++tmp; 74 if (start == tmp) 75 { 76 flag = 1; 77 } 78 if (tmp == start+2*c-1) 79 { 80 break; 81 } 82 if (flag) 83 { 84 printf(" %d", i); 85 } 86 } 87 } 88 } 89 } 90 91 92 printf("\n\n"); 93 } 94 95 return 0; 96 }