poj 1595 Prime Cuts
2012-06-07 09:14
211 查看
//给出一个数,需要你求出其区间中的所有素数,然后再分素数个数的奇偶性,从素数列表的中间开始输出即可! #include <iostream> #include <cmath> #include <memory.h> using namespace std; int primer[1000]; bool is_primer(int n) { if (n == 1) return 1; int tmp = sqrt(float(n)); for (int i = 2; i <= tmp; i++) if (n % i == 0) return 0; return 1; } int main() { int n, c, i, j, pos, num; while (cin >> n >> c) { memset(primer, 0, sizeof(primer)); j = 1; for (i = 1; i <= n; i++) { if (is_primer(i)) { primer[j] = i; j++; } } cout << n << " " << c << ":"; num = 0; if (c*2 > j)//第一次WA,没有分情况讨论,如果要求输出的个数大于它本身拥有的素数的,就全部输出 { for (i = 1; i < j; i++) cout << " " << primer[i]; cout << endl << endl;//第二次WA,没有输出空行! } else { if (j%2==0) { pos = (j-c*2)/2; for (i = pos+1; ; i++) { num++; if (num > c*2-1) break; if (primer[i] != 0) cout << " " << primer[i]; } cout << endl << endl; } else { pos = (j-c*2)/2; for (i = pos+1; ; i++) { num++; if (num > c*2) break; if (primer[i] != 0) cout << " " << primer[i]; } cout << endl << endl; } } } system("pause"); }
相关文章推荐
- POJ 1595 -- Prime Cuts
- poj 1595 Prime Cuts
- POJ-1595-Prime Cuts
- POJ 1595 Prime Cuts
- POJ 1595:Prime Cuts
- poj 1595 Prime Cuts
- poj1595-Prime Cuts
- POJ 1595 Prime Cuts
- POJ 1595 Prime Cuts
- POJ 1595 Prime Cuts
- POJ 3518 Prime Gap 素数
- POJ 3292-Semi-prime H-numbers(H半素数)
- POJ 3126-Prime Path-广度优先搜索bfs
- poj - 3126 Prime Path
- POJ_2689_Prime Distance(区间素数)
- POJ 3518 Prime Gap(素数题)
- poj 3361 Gaussian Prime Factors 高斯素数约数
- PoJ 1595 PrimeCuts
- poj-3126-Prime Path-BFS
- 【POJ 3641】Pseudoprime numbers