首页 > 代码库 > 题目1459:Prime ring problem(素数环问题——递归算法)
题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
//// 1459 Prime ring problem.cpp// Jobdu//// Created by PengFei_Zheng on 23/04/2017.// Copyright © 2017 PengFei_Zheng. All rights reserved.// #include <stdio.h>#include <iostream>#include <algorithm>#include <string.h>#include <cmath>#define PRIME 13#define MAX_SIZE 21using namespace std; int n;int ans[MAX_SIZE];bool used[MAX_SIZE]; int prime[]={2,3,5,7,11,13,17,19,23,29,31,37,41};bool judge(int x){ for(int i = 0 ; i < PRIME ; i++){ if(prime[i]==x) return true; } return false;} void check(){ if(!judge(ans[n]+ans[1])) return; for(int i = 1 ; i <= n ; i++){ if(i!=1) printf(" "); printf("%d",ans[i]); } printf("\n");} void DFS(int num){ if(num>1){ if(!judge(ans[num] + ans[num - 1])) return; } if(num==n){ check(); return; } for(int i = 2 ; i <= n ; i++){ if(!used[i]){ used[i] = true; ans[num+1] = i; DFS(num+1); used[i] = false; } }} int main(){ int kase = 0; while(scanf("%d",&n)!=EOF){ kase++; memset(used,false,sizeof(used)); ans[1] = 1; used[1]=true; printf("Case %d:\n",kase); DFS(1); printf("\n"); } return 0;}/************************************************************** Problem: 1459 User: zpfbuaa Language: C++ Result: Accepted Time:590 ms Memory:1520 kb****************************************************************/
题目1459:Prime ring problem(素数环问题——递归算法)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。