zoukankan      html  css  js  c++  java
  • [Swust OJ 799]--Superprime Rib(DFS)

    题目链接:http://acm.swust.edu.cn/problem/799/

    Time limit(ms): 1000        Memory limit(kb): 10000
     

    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.


    Input

    A single line with the number N.

     
    Output

    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

     
    Hint
    USACO
     

    题目大意:农民约翰的母牛总是生产出最好的肋骨。你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们。
         农民约翰确定他卖给买方的是真正的质数肋骨, 是因为从右边开始切下肋骨, 每次还剩下的肋骨上的数字都组成一个质数,
         举例来说:7331,全部肋骨上的数字 7331是质数; 三根肋骨 733是质数; 二根肋骨 73 是质数; 当然, 最后一根肋骨 7 也是质数。
         7331 被叫做长度 4 的特殊质数。
         写一个程序对给定的肋骨的数目 N(1 <= N <= 8), 求出所有的特殊质数。数字1不被看作一个质数。

    解题思路:由于分割了每一位都是素数,那么首字母只能是2 3 5 7 dfs搜索即可,我打素数表存储素数,居然Runtime Error也是够了

         就原始方法判断素数吧Orz~~~

    代码如下:

     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 
     5 int n;
     6 //#define maxn 100000010
     7 //int n, prime[maxn] = { 1, 1, 0 };
     8 //void init(){
     9 //  int i, j;
    10 //  for (i = 2; i <= maxn; i++){
    11 //      if (!prime[i]){
    12 //          for (j = 2; i*j <= maxn; j++)
    13 //              prime[i*j] = 1;
    14 //      }
    15 //  }
    16 //}
    17 bool isprime(int num)//判断质数
    18 {
    19     if (num == 2)return true;
    20     if (num & 1){
    21         int n = (int)sqrt(num);
    22         for (int i = 3; i <= n; i += 2)
    23         if (!(num%i))
    24             return false;
    25         return true;
    26     }
    27     return false;
    28 }
    29 void dfs(int num, int limit){
    30     int ans;
    31     if (n == limit){
    32         cout << num << endl;
    33         return;
    34     }
    35     for (int i = 1; i <= 9; i += 2){
    36         ans = num * 10 + i;
    37         if (isprime(ans))//if (!prime[ans])
    38             dfs(ans, limit + 1);
    39     }
    40 }
    41 int main()
    42 {
    43     //init();
    44     cin >> n;
    45     dfs(2, 1);
    46     dfs(3, 1);
    47     dfs(5, 1);
    48     dfs(7, 1);
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    开源数据汇集工具
    scrapy定时执行抓取任务
    xpath的常见操作
    ubuntu 安装python mysqldb
    sudo: /etc/sudoers is owned by uid 755, should be 0
    ubuntu 14.04安装mysql数据库
    win7 远程桌面连接centos 6.5
    本地启动spark-shell
    ubuntu 安装 2.10.x版本的scala
    unfolding maps支持中文
  • 原文地址:https://www.cnblogs.com/zyxStar/p/4593364.html
Copyright © 2011-2022 走看看