zoukankan      html  css  js  c++  java
  • NYOJ 素数环

    素数环

    时间限制:1000 ms  |  内存限制:65535 KB
    难度:2
     
    描述

    有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环。

    为了简便起见,我们规定每个素数环都从1开始。例如,下图就是6的一个素数环。

     
    输入
    有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束。
    输出
    每组第一行输出对应的Case序号,从1开始。
    如果存在满足题意叙述的素数环,从小到大输出。
    否则输出No Answer。
    样例输入
    6
    8
    3
    0
    样例输出
    Case 1:
    1 4 3 2 5 6
    1 6 5 2 3 4
    Case 2:
    1 2 3 8 5 6 7 4
    1 2 5 8 3 4 7 6
    1 4 7 6 5 8 3 2
    1 6 7 4 3 8 5 2
    Case 3:
    No Answer
    代码如下:
    #include <stdio.h> 
    #include <stdlib.h>
    #include <math.h>
    
    int primes[40] = {0};
    
    bool IsPrime(int n)
    {
        if (n == 2)
        {
            return true;
        }
        for (int i = 2; i <= sqrt((double)n) + 1; i++)
        {
            if (n % i == 0)
            {
                return false;
            }
        }
        return true;
    }
    
    void DFS(int n, int result[], int nresult, int visited[], int &b)
    {
        if (nresult >= 1 && !IsPrime(result[nresult - 1] + result[nresult % n]))
        {
            return;
        }
        if (nresult == n - 1)
        {
            if (!primes[result[nresult] + result[0]])
            {
                return;
            }
            for (int i = 0; i < n - 1; i++)
            {
                printf("%d ", result[i]);
            }
            printf("%d\n", result[n - 1]);
            ++b;
            return;
        }
        for (int i = 1; i < n; i++)
        {
            if (!visited[i])
            {
                visited[i] = 1;
                result[nresult + 1] = i + 1;
                DFS(n, result, nresult + 1, visited, b);
                visited[i] = 0;
            }
        }
    }
    
    
    int main()
    {
        int i;
        int ncount = 0;
        int visited[25];
        int n;
        int result[25];
        int b = 0;
        for (i = 3; i < 40; i++)
        {
            if (IsPrime(i))
            {
                primes[i] = 1;
            }
        }
        while(1)
        {
            scanf("%d", &n);
            if (0 == n)
            {
                break;
            }
            if (n == 1)
            {
                printf("Case %d:\n", ++ncount);
                printf("1\n");
                continue;
            }
            if (n % 2 == 1)
            {
                printf("Case %d:\n", ++ncount);
                printf("No Answer\n");
                continue;
            }
            result[0] = 1;
            for (i = 0; i <= n; i++)
            {
                visited[i] = 0;
            }
            visited[0] = 1;
            printf("Case %d:\n", ++ncount);
            b = 0;
            DFS(n, result, 0, visited, b);
            if (!b)
            {
                printf("No Answer\n");
            }
        }
        return 0;
    }
  • 相关阅读:
    CF1454F Array Partition
    leetcode1883 准时抵达会议现场的最小跳过休息次数
    leetcode1871 跳跃游戏 VII
    leetcode1872 石子游戏VIII
    CF1355C Count Triangles
    CF1245D Shichikuji and Power Grid
    CF1368C Even Picture
    CF1368D AND, OR and square sum
    CF1395C Boboniu and Bit Operations
    SpringBoot和开发热部署
  • 原文地址:https://www.cnblogs.com/lzmfywz/p/3055967.html
Copyright © 2011-2022 走看看