标签:
#include <iostream> #include <cstring> using namespace std; bool prime[41]={0,0,1,1,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0}; int vis[25]; int a[25]; int n; int p=1; void dfs(int num) { if(num==n&&prime[a[num-1]+a[0]]) { cout<<a[0]; for(int i=1;i<n;i++) cout<<" "<<a[i]; cout<<endl; } else { for(int i=2;i<=n;i++) { if(!vis[i]&&prime[a[num-1]+i]) { vis[i]=1; a[num]=i; dfs(num+1); vis[i]=0; } } } } int main() { while(cin>>n) { memset(vis,0,sizeof(vis)); cout<<"Case "<<p++<<":"<<endl; a[0]=1; dfs(1); cout<<endl; } return 0; }
代码如上 就是一个理解 怎么输出的顺序问题
标签:
原文地址:http://www.cnblogs.com/luosuo10/p/5193718.html