Vijos 1359题:Superprime
2017-04-27 13:59
239 查看
描述
农民约翰的母牛总是生产出最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。农民约翰确定他卖给买方的是真正的质数肋骨,是因为从右边开始切下肋骨,每次还剩下的肋骨上的数字都组成一个质数,举例来说:
7 3 3 1
全部肋骨上的数字 7331是质数;三根肋骨 733是质数;二根肋骨 73 是质数;当然,最后一根肋骨 7 也是质数。
7331 被叫做长度 4 的特殊质数。
写一个程序对给定的肋骨的数目 N (1<=N<=8),求出所有的特殊质数。数字1不被看作一个质数。
格式
输入格式单独的一行包含N。
输出格式
按顺序输出长度为 N 的特殊质数,每行一个。
并按大小顺序排列(从小到大).
样例1
样例输入14
样例输出1
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
限制
每个测试点1秒提示
很简单,不要先算出来在交表-_-~~#include <iostream> #include <cmath> using namespace std; int n; bool primes(int n) { if (n == 1) return false; else if (n == 2) return true; else { for (int i = 2; i <= sqrt(n); i++) if (n % i == 0) return false; return true; } } void search(int value, int index) { if (!index && primes(value)) { cout << value << endl; return; } if (primes(value)) { search(value * 10 + 1, index - 1); search(value * 10 + 3, index - 1); search(value * 10 + 5, index - 1); search(value * 10 + 7, index - 1); search(value * 10 + 9, index - 1); } } int main() { cin >> n; search(2, n - 1); search(3, n - 1); search(5, n - 1); search(7, n - 1); }
相关文章推荐
- Vijos—— T 1359 Superprime
- Vijos1359_Superprime
- VIJOS-P1359 Superprime
- [32] Vijos P1359 Superprime(深搜)
- 116 Super-Prime(完全背包+打表预处理)
- USACO Superprime Rib
- 116. Index of super-prime
- Superprime Rib(usaco)
- usaco Superprime Rib
- usaco Superprime Rib
- 寒假集训——Index of super-prime 和 北航9届校赛晴天小猪爱61
- SGU 116 Index of super-prime
- 2428: Super Prime 求超级素数
- USACO SuperPrime Rib
- usaco-sprime-superprime-pass
- USACO Superprime Rib
- usaco Super Prime Rib individual report
- sdut oj 2404 super prime(素数筛)
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
- 【SGU】116. Index of super-prime 完全背包