zoukankan      html  css  js  c++  java
  • Prime is problem

    题目描述:

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
    Note: the number of first circle should always be 1.


    输入:

    n (1 < n < 17).

    输出:

    The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
    You are to write a program that completes above process.
    Print a blank line after each case.

    样例输入:
    6
    8
    
    样例输出:
    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

    为了解决该问题,我们可以采用回溯法枚举每一个值。当第一个数位为1确定时,我们尝试放入第二个数,使其和1的和为素数,放入后再尝试放入第三个数,使其与第二个数的和为素数,直到所有的数全部被放入环中,且最后一个数与1的和也是素数,那么这个方案即为答案,输出;若在尝试放数的过程中, 发现当前位置无论放置任何之前未被使用的数均不可能满足条件,那么我们回溯 改变其上一个数,直到产生我们所需要的答案,或者确实不再存在更多的解。
    #include "stdafx.h"
    #include <stdio.h>
    using namespace std;
    
    int prime[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37 };
    int number[20];
    bool hash[20];
    int n;
    bool isPrime(int num)
    {
        for (int i = 0; i < 12;i++)
            if (num == prime[i])
                return true;
        return false;
    }
    
    void printArray()
    {
        if (isPrime(number[n] + number[1]) == false)
            return;
        for (int i = 1; i <= n; i++)
        {
            if (i != 1)
                printf(" ");
            printf("%d", number[i]);
        }
        printf("
    ");
    }
    
    void DFS(int num)
    {
        if (num > 1 && isPrime(number[num] + number[num - 1]) == false)
            return;
        if (num == n)
        {
            printArray();
            return;
        }
    
        for (int i = 2; i <= n; i++)
        {
            if (hash[i] == false)
            {
                hash[i] = true;
                number[num + 1] = i;
                DFS(num + 1);
                hash[i] = false;
            }
        }
    }
    
    int main()
    {
        int cas = 0;
        while (scanf("%d", &n) != EOF)
        {
            cas++;
            for (int i = 0; i < 20; i++)
                hash[i] = false;
            number[1] = 1;
            printf("Case %d:
    ", cas);
            hash[1] = true;
            DFS(1);
            printf("
    ");
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    python unittest--TestSuit类--通过unittest.TestSuite()类直接构建,或者通过TestSuite实例的addTests、addTest方法构建
    Cannot read property 'toLowerCase' of undefined
    Vue 中登录功能的简单实现
    git 常用命令
    js 锚点定位的简单方法
    Vue element-ui 使用Loading服务按需引入的坑
    防抖 节流
    element-ui 日期选择器-开始结束日期选择限制
    vue elment-ui 对navTab导航组件封装
    vue 监听窗口变化对页面部分元素重新渲染
  • 原文地址:https://www.cnblogs.com/tgycoder/p/5033291.html
Copyright © 2011-2022 走看看