素数环 南阳acm488(回溯法)

素数环

时间限制: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
来源
hdu改编
上传者
ACM_丁国强

 1 #include<stdio.h>
 2 #include<string.h>
 3 int a[99],b[99],c[99];
 4 //a[99]数组用来放0,1是奇数就放1反之,b[99]数组用判断1~n个数中是否放入c[99]数组里放入就1反之,c[99]数组用来存放1~n;
 5 int f(int x)    //用来判断是否为素数,是就返回1
 6 {
 7     int i;
 8     for(i=2;i*i<=x;i++)
 9     {
10         if(x%i==0)
11             return 0;
12     }
13     return 1;
14 }
15 void f1(int x,int n)
16 {
17     int i;
18     if(x==n&&a[c[0]+c[n-1]])    //如果c[0]+[n-1](也就是头尾相加)也为奇数时满足
19     {
20         for(i=0;i<n;i++)
21             printf("%d ",c[i]);
22         printf("\n");
23         return;
24     }
25     for(i=2;i<=n;i++)
26     {
27         if(!b[i]&&a[i+c[x-1]])    //当前的i+c[x-1](也就是i+它上一个相连的数)也为奇数时
28         {
29             c[x]=i;
30             b[i]=1;
31             f1(x+1,n);    //递归
32             b[i]=0;    //回溯
33         }
34     }
35 }
36 int main()
37 {
38     int i,n,ans;
39     ans=1;
40     for(i=2;i<=40;i++)
41         a[i]=f(i);
42     while(scanf("%d",&n)!=EOF,n)
43     {
44         memset(b,0,sizeof(b));
45         c[0]=1;        //第一个位置放1;
46         if(n==1)    //特殊情况
47         {
48             printf("Case %d:\n1\n",ans++);
49             continue;
50         }
51         if(n%2==1)    //奇数不行
52         {
53             printf("Case %d:\nNo Answer\n",ans++);
54             continue;
55         }
56         else
57         {
58             printf("Case %d:\n",ans++);
59             f1(1,n);
60             continue;
61         }
62     }
63     return 0;
64 }

原文地址:https://www.cnblogs.com/-skyblue/p/9346244.html

时间: 2024-08-25 19:57:36

素数环 南阳acm488(回溯法)的相关文章

UVA - 524 Prime Ring Problem(素数环)(回溯法)

题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<s

UVA 524 素数环 【dfs/回溯法】

Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,3,...,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 alwa

素数环——搜索与回溯

题目描述 Description 从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数. 输入输出格式 Input/output 输入格式:无输入输出格式:一个整数(第几号素数环),一列数字(表示这个素数环) 输入输出样例 Sample input/output 样例测试点#1 输入样例: 无 输出样例: 这里就不演示了(…) 思路:代码很清楚,此处无需讲~~ 代码如下: 1 #include <stdio.h> 2 #include <math.h> 3 int a[3

素数环(递归 搜索 回溯)

素数环:从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数. 分析:首先设有20个空位,填进20个数,可以填数的条件是这个数在之前没有出现过,并且与它的前一个数的和是 一个素数:注意第二十个数时还要判断与第一个数的和是否是一个素数.若满足条件,填数,否则填下一个数: 代码如下: #include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>using namespace std

ACM:回溯法,八皇后问题,素数环

(一)八皇后问题 (1)回溯法 #include <iostream> #include <string> #define MAXN 100 using namespace std; int tot = 0, n = 8; int C[MAXN]; void search(int cur) { if(cur == n) ++tot; //递归边界,只要走到了这里,所有皇后必然不冲突 else for(int i = 0; i < n; ++i) { int ok = 1; C

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

素数环问题

一. 问题描述 把从1到n(n>=2)这n个数摆成一个环,要求相邻的两个数的和是一个素数,找出所有满足条件的环. 二. 问题分析 1> 解向量:<x1, x2, ··· , xn> 2> 解空间树:排列树,(n-1)!个叶子结点 3> 剪枝函数:isPrime( x[t-1]+x[t] ),t=2,3,···,n  约束函数 三. 算法实现 #include <iostream> #include <cmath> using namespace

17图的搜索算法之回溯法

回 溯 法 回溯算法实际是一个类似枚举的搜索尝试方法,它的主题思想是在搜索尝试中找问题的解,当不满足求解条件就"回溯"返回,尝试别的路径.回溯算法是尝试搜索算法中最为基本的一种算法,其采用了一种"走不通就掉头"的思想,作为其控制结构. [例1]八皇后问题模型建立 要在8*8的国际象棋棋盘中放八个皇后,使任意两个皇后都不能互相吃掉.规则:皇后能吃掉同一行.同一列.同一对角线的任意棋子.如图5-12为一种方案,求所有的解. 模型建立 不妨设八个皇后为xi,她们分别在第i

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