Q素数环问题

Description

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. 


 

Input

n (0 < n < 20). 
 

Output

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. 
 

Sample Input

    
    
6 8
 

Sample Output

    
    
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

 

题目分析

给你一个数n,要构成一个素数环,这个素数由1-n组成,它的特征是选中环上的任意一个数字i,i与它相连的两个数加起来都分别为素数,满足就输出。

解题思路

dfs深搜全部的搜索一遍就行,也可以将素数全部打表记录,可以一定程度的节省时间

源代码

#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<cstring>
using namespace std;
int vis[21],maze[21];
int n,k=1;
bool pri(int a,int b)
{
    int c=a+b;
    for (int i=2;i<=sqrt(c);++i)
    {
        if (c%i==0)
        return false;
    }
    return true;
}
void DFS(int cur)
{
     if (cur==n&&pri(1,maze[n-1]))
     {
             cout<<maze[0];
             int i;
             for(i=1;i<n;++i)
             {
                 cout<<' '<<maze[i];
             }
             cout<<endl;
     }
     else
     {
         int i;
         for (i=2;i<=n;++i)
         {
             if(true==vis[i]||!pri(maze[cur-1],i))
             {
                      continue;
             }
             maze[cur]=i;
             vis[i]=true;
             DFS(cur+1);
             vis[i]=false;
         }
     }
}


int main()
{
    maze[0]=1;
    memset(vis,false, sizeof(vis));
    while (cin>>n)
    {
          cout<<"Case "<<k++<<':'<<endl;
          DFS(1);
          cout<<endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
素数问题是指将1到n的n个数排列成一个,使得相邻两个数之和都是素数。其中,n为偶数且大于等于4。要解决这个问题,我们可以使用队列(queue)数据结构。 具体思路如下: 1. 将所有素数存入一个数组prime[]中; 2. 将第一个数1入队列; 3. 从2开始,逐个考虑所有素数,如果当前素数与队列的最后一个数之和为素数,则将该素数入队列; 4. 如果当前素数与队列的最后一个数之和不为素数,则弹出队列的最后一个数,继续判断; 5. 如果队列中的元素个数等于n,则输出队列中所有数字的排列方式。 下面是C++代码实现: ```c++ #include <iostream> #include <queue> using namespace std; const int MAX_N = 20; int n; bool isPrime[MAX_N * 2]; // 判断一个数是否为素数 int prime[MAX_N * 2]; // 存放所有素数 bool used[MAX_N + 1]; // 判断一个数是否被使用过 queue<int> q; // 存放已经找到的数字序列 void sieve(int n) { // 筛选出素数 for (int i = 2; i <= n; i++) { isPrime[i] = true; } for (int i = 2; i * i <= n; i++) { if (isPrime[i]) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } } } void makePrime(int n) { // 将素数存入数组中 int cnt = 0; for (int i = 2; i <= n; i++) { if (isPrime[i]) { prime[cnt++] = i; } } } void search() { if (q.size() == n) { // 如果数字序列中的元素个数等于n if (isPrime[q.front() + q.back()]) { // 判断第一个数和最后一个数之和是否为素数 for (int i = 0; i < n; i++) { cout << q.front() << " "; // 输出数字序列 q.pop(); } cout << endl; } } else { for (int i = 1; i < n; i++) { // 枚举所有可能的数字 if (!used[i] && isPrime[i + q.back()]) { // 如果当前数字没有被使用过且与队列最后一个数字之和为素数 q.push(i); // 将数字入队列 used[i] = true; // 标记当前数字已经被使用 search(); // 递归搜索下一个数字 used[i] = false; // 恢复当前数字的使用状态 q.pop(); // 弹出当前数字 } } } } int main() { cin >> n; sieve(n * 2); // 筛选出所有素数 makePrime(n * 2); // 将素数存入数组中 q.push(1); // 将数字1入队列 used[1] = true; // 标记数字1已经被使用 search(); // 开始搜索 return 0; } ``` 在上面的代码中,我们首先使用筛法求出了所有小于等于2n的素数,并将它们存入数组prime[]中;然后,我们使用队列q存放已经找到的数字序列。在search()函数中,我们使用递归的方式进行搜索。如果数字序列中的元素个数等于n,则输出数字序列;否则,我们枚举所有可能的数字,并判断当前数字是否满足题目要求,如果满足则将它入队列,并递归搜索下一个数字。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值