zoukankan      html  css  js  c++  java
  • USACO1.6 Superprime Rib

    题目传送门

    每一个特殊质数都会被从右边切掉,所以除了首位外的其它位数一定都不会是偶数,只能是$1$,$3$,$5$,$7$,$9$

    而每一个特殊质数的首位一定是质数,也就是$2$,$3$,$5$,$7$这四个。

    然后大体思路就是一直不停地往初始数上加$1$,$3$,$5$,$7$,$9$,判断是不是质数就可以了。

    要做到这些,一个$dfs$就可以了,还是用$for$枚举的话,代码量会有点大。

    顺序枚举就可以保证按大小顺序输出。

     1 /*
     2 ID: Starry21
     3 LANG: C++
     4 TASK: sprime                 
     5 */  
     6 #include<cstdio>
     7 #include<cmath>
     8 using namespace std;
     9 int n,digit[5]={1,3,5,7,9};
    10 bool check(int x)
    11 {
    12     if(x==1) return 0;
    13     for(int i=2;i<=sqrt(x);i++)
    14         if(x%i==0)
    15             return 0;
    16     return 1;
    17 }
    18 void dfs(int p,int k)
    19 {
    20     if(k==n)
    21         printf("%d
    ",p);
    22     for(int i=0;i<5;i++)
    23     {
    24         int t=p*10+digit[i];
    25         if(!check(t)) continue;
    26         dfs(t,k+1);
    27     }
    28 }
    29 int main()
    30 {
    31     freopen("sprime.in","r",stdin);
    32     freopen("sprime.out","w",stdout);
    33     scanf("%d",&n);
    34     dfs(2,1);
    35     dfs(3,1);
    36     dfs(5,1);
    37     dfs(7,1);
    38     return 0;
    39 }
    Code

     官方题解也是这个思路,实现比较相似。

    转载请注明出处,有疑问欢迎探讨 博主邮箱 2775182058@qq.com
  • 相关阅读:
    flutter 右滑返回上一页
    flutter 的Animation简单了解
    Flutter Offstage、Visibility隐藏/可见
    flutter手势
    Flutter生命周期
    flutter 路由动画
    flutter 保持页面状态
    flutter 不规则底部工具栏实现
    flutter 主页面底部导航栏实现以及主题风格设置
    flutter DropdownButton使用
  • 原文地址:https://www.cnblogs.com/lyttt/p/11644047.html
Copyright © 2011-2022 走看看