HDU-1016 prime ring problem

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

以前做过此题,几天又复习一遍。

/*
 * HDU-1016 prime ring
 * mike-w
 * 2011-8-30
 * --------------------------------------
 * i met this problem for the second time
 * practice DFS
 * --------------------------------------
 * 看来用C提交比较不靠谱,还得GCC
 * 题目的样例输出把最后一行的换行符省略了
 * 害得我好几次格式错误
 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 22

char tag[MAXSIZE];
char prm[MAXSIZE];
int rec[MAXSIZE];
int n,ccase;

int print(int nele)
{
	int i;
	for(i=1;i<=nele;i++)
		printf("%d%c",rec[i],(i==nele?'\n':' '));
	return 0;
}

int search(int cnt)
{
	int i;

	if(cnt==n)
	{
		for(i=1;i<=n;i++)
			if(!tag[i]&&prm[i+rec[cnt-1]]&&prm[i+1])
			{
				rec[cnt]=i;
				print(n);
			}
	}
	else
		for(i=1;i<=n;i++)
			if(!tag[i]&&prm[i+rec[cnt-1]])
			{
				rec[cnt]=i;
				tag[i]=1;
				search(cnt+1);
				tag[i]=0;
			}
	return 0;
}

int main(void)
{
	ccase=1;
	prm[2]=prm[3]=prm[5]=prm[7]=prm[11]
	=prm[13]=prm[17]=prm[19]=prm[23]
	=prm[29]=prm[31]=prm[37]=prm[41]=1;

#ifndef ONLINE_JUDGE
	freopen("1016.in","r",stdin);
#endif

	while(scanf("%d",&n)!=EOF)
	{
		memset(tag,0,sizeof(tag));
		tag[1]=1;
		rec[1]=1;
		printf("Case %d:\n",ccase++);
		search(2);
		putchar('\n');
	}
	return 0;
}


 

HDU 1016Prime Ring Problem(DFS、素数筛) 思路: 要求每两个相邻数之和都为素数,那么只要每次递归调用DFS时判断之前两个数之和是否为素数,另外注意因为是一个环,最后还要判断一下最后一个数和第一个数之和是否为素数。 程序开始时求出一个素数数组,只需求出50以内的素数即可,因为数据<20,最大的素数和为17+19=36。此处用的是素数筛法(什么是素数筛法?)求素数。 素数筛法概览: 把从1开始的、某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉 阅读详情

相关推荐

hdu 1016 Prime Ring Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem Description A ring is compose of n ci...

qq_45852004的博客 148

hdu1016 Prime Ring Problem

hdu1016 Prime Ring Problem ...................... 1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <queue> 5 #i...

weixin_30820151的博客 90

深搜经典题型 hdu1016 Prime Ring Problem

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K Problem Description A ring is compose of n circles as ...

weixin_43732535的博客 226

HDOJ 1016 HDU 1016 Prime Ring Problem ACM 1016 IN HDU

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1016题目描述:Prime Ring ProblemTime Limit: 4000/2000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6150Accepted...

weixin_30483697的博客 132

HDU-1016 Prime Ring Problem(深搜)

HDU-1016 Prime Ring 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...

碳酸钙的01妖精的博客 208

HDU 1016 Prime Ring Problem

HDU 1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27785    Accepted Submission(s): 12345 Problem Description A ri

Spectacules的博客 427

hdu1016Prime Ring Problem

http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11874    Accepted Submissi

hnust_xiehonghao的专栏 718

hdu 1016 Prime Ring Problem

hdu  1016  Prime Ring Problem             题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 DFS 题目大意:输入一个数,将自然数1~此数排成一个圈,使每两个相邻的数相加之和为素数。 题目分析:标准DFS。 code; #include #include int p[40],vis[20],r

slicer的技术交流栈 705

Prime Ring Problem HDU - 1016

素数环 #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; int n; int a[30]; int v[30]; bool prime( int x ){ for( int i = 2 ; i <= sqrt(x) ; i++ ) if( x % i =

qq_43627860的博客 129

[HDU 1016]--Prime Ring Problem(回溯)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others) Problem Description A ring is compose ...

weixin_34342207的博客 102

HDU-1016 Prime Ring Problem(DFS深搜+打表)

题目回顾(HDU-1016): Prime Ring Problem Problem 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...

weixin_30339969的博客 106

HDU-1016-Prime Ring Problem

HDU-1016-Prime Ring Problem http://acm.hdu.edu.cn/showproblem.php?pid=1016 基本的DFS,先打个素数表即可 #include #include #include int prime[50]; int visit[30]; int num[30]; int n; void init() { int i,j;

菜鸟的程序人生 1150

HDU 1016 prime ring 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

weixin_37483932的博客 320
上一篇: HDU-1019 least common multiple
下一篇: RQNOJ-04 数列
creativewang
博客等级 码龄17年 20粉丝 203原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值