USACO Section 1.5 Superprime Rib

题目描述

屠宰农民约翰的牛总是产生最好的主肋骨。 您可以通过查看FJ和USDA一个接一个地密切盖在他们之间的数字来判断排骨。 农夫约翰确保他的主肋骨的购买者获得真正的排骨,因为从右边切片时,肋骨上的数字继续保持原始状态,直到最后一个肋骨,例如:7 3 3 1
由7331表示的一组肋是素数; 三条肋骨733是素质; 两个肋73是素,当然,最后的肋7是素。 数字7331被称为长度为4的超级时代。
编写一个程序,接受数字N 1 <= N <= 8的肋条,并打印所有超级时间的长度。
数字1(本身)不是素数。

程序名称:sprime
输入格式

数字N的单行

输入(file sprime.in)

 
 
4

输出格式

长度为N的超级肋骨以每行一个升序排列。

输出(file sprime.out)

2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393

解题代码

/*
ID: 15189822
PROG: sprime
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
ifstream fin("sprime.in");
ofstream fout("sprime.out");
int cnt=1,N;
long k=10;
bool is_prime(long n){
   if (n==0||n==1) return false;
   if (n==2||n==3) return true;
   long i;
   for (i=2;i<=sqrt(n);i++){
      if (n%i==0) return false;
   }
   return true;
}
void dfs(long n){
    if (!is_prime(n)||cnt>N) return ;
    if (cnt==N){
       fout<<n<<endl;
       return ;
    }
    for (int i=1;i<=9;i+=2){
        cnt++;
        n=n*k+i;
        dfs(n);
        n=(n-i)/k;
        cnt--;
    }
}
int main(){
    fin>>N;
    cnt=1;
    dfs(2);
    cnt=1;
    dfs(3);
    cnt=1;
    dfs(5);
    cnt=1;
    dfs(7);
}




猜你喜欢

转载自blog.csdn.net/YanLucyqi/article/details/77370447