比赛 20220531高一小测验 评测结果 AWAWAAAAAA
题目名称 素数环 最终得分 80
用户昵称 dew52 运行时间 0.635 s
代码语言 C++ 内存使用 3.44 MiB
提交时间 2022-06-01 20:56:05
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 20 + 5;
int n, x;//长度为n的环 
int a[MAXN];//输入数组 
bool vis[MAXN];//标记数组 
void dfs (int x);
void print();
int p[45] = {0};//素数数组 
void init();
int main()
{
    freopen("primering.in","r",stdin);
    freopen("primering.out","w",stdout);
    init();
    int i = 0;
    while (scanf("%d", &n) != EOF)
    {
        a[1] = 1;
        i++;
        printf("Case %d:\n", i);
        switch(n)
        {
            case 3:
            case 5:
            case 7:
            case 9:
            case 11:
            case 13:
            case 15:
            case 17:
            case 19:
                puts("");
                continue;
        }
        dfs (1);
        puts("");
    }
    return 0;
}
void dfs (int x)
{
    if (x == n && p[a[x] + 1])
    {
        print();
        return;
    }
    for (int i = 2; i <= n; ++i)
    {
        if (vis[i]) continue;//没查过 
        if (p[a[x] + i])//是素数 
        {
            a[x + 1] = i;
            vis[i] = true;
            dfs(x + 1);
            a[x + 1] = 0;
            vis[i] = false;
        }
    }
}
void print()
{
    for (int i = 1; i <= n; ++i)
    {
        cout << a[i] << " ";
    }
    puts("");
}
void init()
{
    p[2] = 1;
    p[3] = 1;
    p[5] = 1;
    p[7] = 1;
    p[11] = 1;
    p[13] = 1;
    p[17] = 1;
    p[19] = 1;
    p[23] = 1;
    p[23] = 1;
    p[29] = 1;
    p[31] = 1;
    p[37] = 1;
    p[41] = 1;
}