题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列

照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<algorithm>
#define mod=1e9+7;
using namespace std; typedef long long LL;
int a[],vis[],isp[];
int n; void is_prime(){
isp[]=isp[]=;
for(int i=;i<=;i++){
if(isp[i]==){
for(int j=i*;j<=;j+=i)
isp[j]=;
}
}
} void dfs(int cur){
int i;
if(cur==n&&!isp[a[]+a[n-]]){
for( i=;i<n-;i++) printf("%d ",a[i]);
printf("%d\n",a[i]);
} else for(i=;i<=n;i++)
if(!vis[i]&&!isp[i+a[cur-]]){
a[cur]=i;
vis[i]=;
dfs(cur+);
vis[i]=;
}
} int main(){
int t=;
is_prime();
while(cin>>n){
memset(vis,,sizeof(vis));
a[] = ;
vis[] = ;
if(t) printf("\n");
printf("Case %d:\n", ++t);
dfs();
}
return ;
}

自己写的时候,直接是dfs(0),什么都输不出来,可是为什么是这样呢= = 学习回溯的第一题目 = = 再好好理解- -

最新文章

  1. BZOJ4596: [Shoi2016]黑暗前的幻想乡
  2. centos7安装python3
  3. Objective-c防止数组越界而崩溃(全局效果)
  4. 使用FIR.im发布自己的移动端APP
  5. XStream使用总结
  6. Android - 广播接收者 - BroadcastReceiver
  7. Asp.net MVC中 Controller 与 View之间的数据传递
  8. 关于iis7短日期后面带有星期的解决办法
  9. 关于MVC4中EFCoderFirst 数据迁移的三句经典指令
  10. 看人家如何拿到腾讯阿里的offer
  11. 利用hadoop自带程序运行wordcount
  12. 最新的Android SDK安装攻略(动作要快,来晚就失效了)
  13. 简单总结下opacity、transparent以及rgba
  14. ( 转)Ubuntu下创建、重命名、删除文件及文件夹,强制清空回收站方法
  15. Spark2.1.0——内置RPC框架详解
  16. LOJ 10138 -「一本通 4.5 例 1」树的统计
  17. 循序渐进学.Net Core Web Api开发系列【0】:序言与目录
  18. easyui------修改validatebox过滤规则
  19. Vultr VPS建站攻略 – 一键安装宝塔面板架设LNMP/LAMP Web环境
  20. [T-ARA][Lovey-Dovey]

热门文章

  1. cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)
  2. Python标准库之os模块
  3. [sql server、oracle] 分组取最大值最小值常用sql
  4. Codeforces Round #327 (Div. 1) B. Chip &#39;n Dale Rescue Rangers 二分
  5. firefox同步ajax请求报错的问题 A parameter or an operation is not supported by the underlying object
  6. Unity3d 一些 常见路径
  7. mysql数据库主外键级联删除脚本RESTRICT --&gt; CASCADE
  8. POJ 2251 Dungeon Master(广搜,三维,简单)
  9. ZOJ 3757 Alice and Bob and Cue Sports(模拟)
  10. hdu 4701 Game 博弈论