[Swust OJ 799]--Superprime Rib(DFS)
题目链接:http://acm.swust.edu.cn/problem/799/
Description
Butchering Farmer John's cows always yields the best prime rib. You can tell prime ribs by looking at the digits lovingly stamped across them, one by one, by FJ and the USDA. Farmer John ensures that a purchaser of his prime ribs gets really prime ribs because when sliced from the right, the numbers on the ribs continue to stay prime right down to the last rib, e.g.:
7 3 3 1
The set of ribs denoted by 7331 is prime; the three ribs 733 are prime; the two ribs 73 are prime, and, of course, the last rib, 7, is prime. The number 7331 is called a superprime of length 4.
Write a program that accepts a number N 1 <=N<=8 of ribs and prints all the superprimes of that length.
The number 1 (by itself) is not a prime number.
A single line with the number N.
The superprime ribs of length N, printed in ascending order one per line.
Sample Input
| 4 |
Sample Output
|
2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393
|
题目大意:农民约翰的母牛总是生产出最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。
农民约翰确定他卖给买方的是真正的质数肋骨, 是因为从右边开始切下肋骨, 每次还剩下的肋骨上的数字都组成一个质数,
举例来说:7331,全部肋骨上的数字 7331是质数; 三根肋骨 733是质数; 二根肋骨 73 是质数; 当然, 最后一根肋骨 7 也是质数。
7331 被叫做长度 4 的特殊质数。
写一个程序对给定的肋骨的数目 N(1 <= N <= 8), 求出所有的特殊质数。数字1不被看作一个质数。
解题思路:由于分割了每一位都是素数,那么首字母只能是2 3 5 7 dfs搜索即可,我打素数表存储素数,居然Runtime Error也是够了
就原始方法判断素数吧Orz~~~
代码如下:
#include <iostream>
#include <cmath>
using namespace std; int n;
//#define maxn 100000010
//int n, prime[maxn] = { 1, 1, 0 };
//void init(){
// int i, j;
// for (i = 2; i <= maxn; i++){
// if (!prime[i]){
// for (j = 2; i*j <= maxn; j++)
// prime[i*j] = 1;
// }
// }
//}
bool isprime(int num)//判断质数
{
if (num == )return true;
if (num & ){
int n = (int)sqrt(num);
for (int i = ; i <= n; i += )
if (!(num%i))
return false;
return true;
}
return false;
}
void dfs(int num, int limit){
int ans;
if (n == limit){
cout << num << endl;
return;
}
for (int i = ; i <= ; i += ){
ans = num * + i;
if (isprime(ans))//if (!prime[ans])
dfs(ans, limit + );
}
}
int main()
{
//init();
cin >> n;
dfs(, );
dfs(, );
dfs(, );
dfs(, );
return ;
}
[Swust OJ 799]--Superprime Rib(DFS)的更多相关文章
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 284通过 425提交 题目提供者该用户不存在 标签USACO 难度普及- 提交 讨论 题解 最新讨论 超时怎么办? ...
- USACO 1.5 Superprime Rib
Superprime Rib Butchering Farmer John's cows always yields the best prime rib. You can tell prime ri ...
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 使用四种算法
洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 水题一道…… 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们. ...
- [Swust OJ 404]--最小代价树(动态规划)
题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535 Des ...
- [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)
题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...
- USACO Superprime Rib
洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 洛谷传送门 JDOJ 1673: Superprime Rib JDOJ传送门 题目描述 农民约翰的母牛总是产生最好 ...
- SWUST OJ NBA Finals(0649)
NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128 Descri ...
- [Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)
题目链接:http://acm.swust.edu.cn/problem/465/ 还有一道题只是描述不一样,方法一模一样(http://acm.swust.edu.cn/problem/644/) ...
随机推荐
- C语言2
函数是C语言的基本单位,类是java,c#,c++的基本单位 int abs(int x); double fabs(double x); 按变量的存储方式:静态变量.自动变量.寄存器变量 指针就 ...
- Eclipse中搭建Python开发环境
转载:http://www.cnblogs.com/realh/archive/2010/10/04/1841907.html Eclipse+PyDev环境搭建 1.准备工作 JDK6 Java 开 ...
- HDU 4981 Goffi and Median
题解:排序取中位数,然后与平均数比较即可. #include <cstdio> #include <algorithm> using namespace std; double ...
- 时尚B2B方兴未艾-Maker’s Row 获100万美元种子投资 |华丽志
时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 |华丽志 华丽志 » 网internet, 时尚B2B方兴未艾-Maker's Row 获100万美元种子投资 由 luxeco 发 ...
- maven项目启动
1服务install 2 build (tomcat:run)
- EF 如何code first
首先配置连接数据.sql server <connectionStrings> <add name="Model1" connectionString=" ...
- 转载ajax
写在前面的话: 用了很久的Asp.Net Ajax,也看了段时间的jquery中ajax的应用,但到头来,居然想不起xmlHttpRequest的该如何使用了. 以前记的也不怎么清楚,这次就重新完整的 ...
- 编写一个程序, 将 a.txt 文件中的单词与 b.txt 文件中的 单词交替合并到 c.txt 文件中, a.txt 文件中的单词用回车符 分隔, b.txt 文件中用回车或空格进行分隔。
package cn.itcast; import java.io.File; import java.io.FileReader; import java.io.FileWriter; public ...
- 【转】Qt Mode/View
1.view与Widget 在UI中,最常用的就是list/grid/tree了(在Qt中,grid被称为table).尤其是做那些数据库相关的程序,可能每个界面都要用到 list或grid.在Qt中 ...
- idea中使用sbt构建scala项目及依赖
1.安装scala插件 http://www.cnblogs.com/yrqiang/p/5310700.html 2. 详细了解sbt: http://www.scala-sbt.org/0.13/ ...