[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/) ...
随机推荐
- 7_Table Views
7 // // ViewController.swift // Table Views // // Created by ZC on 16/1/9. // Copyright © 2016年 ZC. ...
- JSP打印九九乘法表
##index.jsp: <%@ page language="java" import="java.util.*" pageEncoding=" ...
- Java学习——Ubuntu下jdk的安装以及Java环境的配置
第一步.下载 到官网下载jdk,在Java SE Development Kit 7uXX里面,把Accept License Agreement 打勾,这样才能下载jdk,然后我们选择对应的Ubun ...
- ios蓝牙开发(二)ios连接外设的代码实现
上一篇文章介绍了蓝牙的技术知识,这里我们具体说明一下中心模式的应用场景.主设备(手机去扫描连接外设,发现外设服务和属性,操作服务和属性的应用.一般来说,外设(蓝牙设备,比如智能手环之类的东西), 会由 ...
- DevExpress ASP.NET 使用经验谈(1)-XPO模型的创建
这个系列通过一些简单例子循序渐进,介绍DevExpress ASP.NET控件的使用.先来介绍一下XPO的使用,安装的DevExpress版本为DXperienceUniversal-12.2.4,使 ...
- connot find one or more components. please reinstall the application
正在用 Visual Studio 2013 写程序,程序一直执行正常. 此时,手动把注册表"HKEY_USERS"的当前用户的权限删除.再运行程序会提示:“是否继续并运行上次的成 ...
- EF的泛型封装 写的很好 转自Fly_Elephant http://www.cnblogs.com/xiaofeixiang/p/4188600.html?utm_source=tuicool
Entity Framework本身的增删改查其实 已经很方便了,不过做项目的时候用的多了也就觉得有点累了,每个业务实体基本上都涉及到到了增删改查这四个基本的要素,至于封装每个公司可能都不一样,接口, ...
- input输入框只能输入数字的功能
Html代码 收藏代码 <input type="text" style="ime-mode:disabled;" onpaste="retur ...
- linux cmd: ps
每天一个linux命令(41):ps命令 http://www.cnblogs.com/peida/archive/2012/12/19/2824418.html Linux中的ps命令是Proces ...
- 不直接用NSLog
公司中不直接使用NSLog,而是利用宏定义自己的打印函数,将该打印函数写在项目的.pch文件中.调试的时候往往用到好多打印,但发布的时候确不需要.(一下是在公司中的一些处理) 自定义NSLog 一,固 ...