HDU 1016 Prime Ring Problem

Scratch离线版部署指南:解决资源缺失与页面显示异常问题 本文提供了一份详尽的Scratch离线版部署指南,旨在解决网络依赖导致的资源缺失与页面显示异常问题。通过从基础文件获取、自动化资源抓取到生产环境部署的完整流程,帮助教育工作者和开发者在无网络环境下搭建功能完整的本地Scratch编辑器,确保编程教学的顺利进行。 阅读详情

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 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

思路分析

题目的意思是说给你一个1、2、3......n的等差数列,以1为起点,按顺时针方向排列数字,每个数字只能出现一次并且相邻的两个数之和是一个素数,求其排列。

典型的一个DFS,即一个一个的往下判断,不符合时就往上退。用递归写时需要弄清楚进入递归和退出递归的关系以及退出递归后要把节点访问标记还原。这个题目还需要注意的地方就是答案可能有多个,因此,输出一个答案之后如何进行下一个不同答案的输出是需要斟酌的地方,

鉴于n最大值只到20,所以判断素数时可以通过打表然后直接查询来提高程序的运行速度。

代码如下

#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <cmath>

#include <cstring>

#include <string>

#include <set>

#include <map>

#include <vector>

#include <list>

#include <stack>

#include <queue>

#include <algorithm>

using namespace std;

int n;

int a[25];

int visited[25];

bool IsPrime(int m)//判断素数

{

    for (int i=2;i<=sqrt(m);i++)

        if (m%i == 0)

          return 0;

    return 1;

}

void DFS(int j)

{

    int i;

    if (j == n && IsPrime(a[0]+a[j-1]))//必须满足首尾之和也为素数

    {

        cout<<a[0];

        for (int k=1;k<n;k++)

            cout<<" "<<a[k];

        cout<<endl;

    }//输出一个答案后会进行下面的判断(此时visited数组全为1,所以一直往上一个递归上退,并把原来访问的标记还原同时判断是否符合条件)

    for (i=2;i<=n;i++)

        if (visited[i] == 0 && IsPrime(a[j-1]+i))

        {

            a[j]=i;

            visited[i]=1;

            DFS(j+1);

            visited[i]=0;//退出上一个递归,并把不符合的节点“弹出”

        }

}

int main()

{

    int cas=1;

    while (scanf("%d",&n) != EOF)

    {

        a[0]=1;

        memset(visited,0,sizeof(visited));

        printf("Case %d:\n",cas++);

        DFS(1);

        cout<<endl;

    }

    return 0;

}

 

 

Qwen-Image-2512 本地部署完全指南:从零开始搭建你的AI绘图工作站 阿里通义团队开源了Qwen-Image-2512模型,这款AI绘图模型在人物真实感、自然细节和文字渲染上表现惊艳,甚至在盲测中超越部分闭源模型。本文提供了详细的本地部署教程,从硬件准备、环境安装到使用ComfyUI运行模型,手把手教你生成第一张AI图片,并分享了进阶技巧和免费在线体验方式。 阅读详情

相关推荐

TSmaster CANFD配置避坑指南:为什么你的PCAN盒不支持最新版本?

本文针对TSmaster配置PCAN硬件进行CANFD通信时的常见问题,提供了详细的避坑指南。重点解析了CANFD配置中TSEG1等关键参数的理解误区,并深入探讨了TSmaster新版本与PCAN硬件驱动之间的兼容性变化,帮助工程师快速定位并解决连接失败或无法发送CANFD报文等问题。

alpha的博客 560

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的博客 152

lumerical-环形谐振器

利用mode求解器优化得到所需的环的总长度,即环形器的半径,然后通过varFDTD得到光场分布和透过率.

m0_61776728的博客 2201

深搜经典题型 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的博客 235

Argoverse数据集可视化/Argoverse-api

argoverse可视化,argoverse-api配制方法

learning_man的博客 9573

ArduPilot代码解析之四 — 姿态和位置控制

ArduPilot采用级联控制架构实现飞行器的姿态和位置管理。该系统通过位置→速度→加速度→姿态→角速率→电机指令的逐级转换,将高级指令转化为底层执行信号。位置控制采用三维独立控制,水平轴与垂直轴分别处理;姿态控制通过角度误差→角速率→电机输出的两层级联实现。系统支持多种输入模式,并内置运动学整形功能确保轨迹平滑。不同飞行器类型(多旋翼、直升机、潜艇等)有专门的调参方案,通过调整PID参数、加速度限制等实现最优控制性能。该架构兼顾控制精度与飞行稳定性,适用于各类自主飞行场景。

rainercool的专栏 838

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的博客 95

Android 13.0 不过gms认证,使用google商店不报没有获得Google认证的问题解决

在13.0的系统rom定制化开发中,在某些时候一个小项目需求用到google商店,但是不想过认证出这笔大费用,就需要 想做一个fingerprint的白名单,然后再联网的时候不弹出没有获得Google认证,正常使用google商店,接下来解决这个问题

安卓兼职framework和app工程师的博客 118

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的博客 136

02-Redis 核心特性与数据结构详解:揭秘高性能的技术内核

本文深度拆解 Redis 高性能的技术内核:揭秘其凭“内存存储 + C 语言开发” 实现微秒级延迟、10 万 + QPS 的性能优势;详解 5 种基础数据类型(String/Hash 等)与 4 种扩展数据类型(Stream/Geospatial 等)的特性及场景;还介绍多语言支持、RDB/AOF 持久化、主从复制 / 哨兵 / 集群等高可用方案,助读者掌握 Redis 技术核心与实践逻辑。

蜗牛老师,专注编程领域 1009

基于Simulink的双PMSM差速驱动转矩同步控制建模示例

本文详细介绍了基于Simulink的双永磁同步电机(PMSM)差速驱动转矩同步控制建模方法。针对AGV/叉车等双电机独立驱动系统的转矩不同步问题,提出主从式+交叉耦合控制策略,在Simulink中实现直线行驶、90°转弯和单轮打滑恢复三种工况的仿真验证。通过动力学建模、参数差异设置、同步误差定义及控制律设计,最终实现同步误差<3%、转弯半径跟踪误差<5%、打滑后100ms内恢复的性能指标。文章特别强调了实际工程中的参数标定、通信同步和故障处理要点,并提供了完整的Simulink建模步骤和工具箱使

xiaoheshang_123的博客 212

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妖精的博客 211

【天工Godwork精品教程】天工3.1.7安装教程(附Godwork完整版下载地址)

本文讲解天工3.1.7安装过程(附Godwork完整版网盘下载地址)。 一、天工3.1.7安装教程 1. 安装GodWork-AT 3.1.7 2. 安装GodWork-AT 3.1.7补丁 3. 安装GodWork-EOS-Setup-2017B-1231 4. 安装GodWork-EOS补丁 5. 运行godwokr软件 6. 生成ZC码 7. 输入ZC码 8. eos插

智慧空间实验室 5310

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的专栏 722

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

【HDU 1016】Prime Ring Problem(DFS、素数筛)

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

Chen_yuazzy的博客 2262

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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的技术交流栈 712

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

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的博客 132

[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的博客 105

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的博客 111

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;

菜鸟的程序人生 1152

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的博客 323

HDU 1016 Prime Ring Problem

原题目链接HDU1016 分类 HDU 素数 DFS 题意 输入一个数n,把1到n的自然数放到一个环里,保证相邻的两个数的和是素数。 样例输入输出 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...

GatesMa's blog 545
上一篇: HDU 2054 A == B ?
下一篇: HDU 2037 今年暑假不AC
Spectacules
博客等级 码龄12年 2粉丝 69原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址