洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题解 递归

题目链接:https://www.luogu.com.cn/problem/P1218

实现代码如下:

#include <bits/stdc++.h>
using namespace std;
int n;
bool check(int a) {
    if (a < 2) return false;
    for (int i = 2; i*i <= a; i ++)
        if (a % i == 0)
            return false;
    return true;
}
void solve(int num, int m) {
    if (m == n) cout << num << endl;
    else {
        for (int i = 1; i < 10; i ++) if (check(num*10+i)) solve(num*10+i, m+1);
    }
}
int main() {
    cin >> n;
    solve(0, 0);
    return 0;
}
posted @ 2020-05-07 16:33  quanjun  阅读(176)  评论(0编辑  收藏  举报