poj1595
来源:互联网 发布:分享淘宝优惠券赚佣金 编辑:程序博客网 时间:2024/09/21 06:17
Prime Cuts
Description
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.
Input
Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.
Output
For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one blank. Each line of output should be followed by a blank line. Hence, your output should follow the exact format shown in the sample output.
Sample Input
21 218 218 18100 7
Sample Output
21 2: 5 7 1118 2: 3 5 7 1118 18: 1 2 3 5 7 11 13 17100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67
Source
South Central USA 1996
代码如下:
#include<stdio.h>#include<string.h>int prime[1010];void judge_prime(void) { memset(prime, 1, sizeof (prime)); for (int i = 2; i <= 1010; i++) if (prime[i]) for (int j = i * i; j <= 1010; j += i) prime[j] = 0;}int main(void) { int n; int c; int is_prime[1010]; judge_prime(); while (scanf("%d%d", &n, &c) == 2) { printf("%d %d:", n, c); int sum = 0; for (int i = 1; i <= n; i++) if (prime[i]) is_prime[++sum] = i; if ((sum) % 2) if (sum <= 2 * c - 1) for (int i = 1; i <= sum; i++) printf(" %d", is_prime[i]); else for (int i = (sum - 2 * c + 1) / 2 + 1, count = 1; count <= 2 * c - 1; i++, count++) printf(" %d", is_prime[i]); else if (sum <= 2 * c) for (int i = 1; i <= sum; i++) printf(" %d", is_prime[i]); else for (int i = (sum - 2 * c) / 2 + 1, t = 1; t <= 2 * c; i++, t++) printf(" %d", is_prime[i]); printf("\n"); printf("\n"); }}
- poj1595
- poj1595
- poj1595
- poj1595
- 【筛法打表】poj1595 Prime Cuts
- Prime Cuts(poj1595)
- poj1595 素数打表
- poj1595-Prime Cuts
- POJ1595 Prime Cuts [暴力打表]
- poj1595 prime cuts(快速筛选)
- poj1595 Prime Cuts (素数筛选)
- 打表法之素数打表POJ1595
- Erlang Rebar 创建工程总结
- 测试多线程对多核cpu的分支预测的影响
- Oracle 函数专题:TO_CHAR
- 用clojure解决euler problem 9
- SQL优化专题:Oracle HINT
- poj1595
- Android入门:文件上传
- 3.求子数组的最大和
- ios开发
- Linux firefox install adobe flash player
- ORACLE 压缩
- linux下设备device_register和驱动driver_register先后注册的影响和关系
- Oracle index rebuild online 与 rebuild 及 drop index 后重建
- log4j分模块分内容配置