//hdu1016
//PE 每组数据最后一行不能有空格
#include <iostream>
#include <cstring>
#include <math.h>
#include <algorithm>
using namespace std;
int n;
bool used[21];
int ans[21];
int prime[50];
void Prime(){
memset(prime, -1, sizeof(prime));
prime[0] = prime[1] = 0;
for(int i = 2; i < 50; i++){
if(prime[i] == -1){
prime[i] = 1;
for(int j = i + i; j < 50; j += i)
prime[j] = 0;
}
}
}
bool dfs(int cnt){
if(cnt == n && prime[ans[n - 1] + ans[0]] == 1){
for(int i = 0; i < n; i++){
if(i == n - 1)
cout << ans[i] << endl;
else
cout << ans[i] << ' ';
}
//返回true的话,只输出第一组数据,要么返回false,要么什么也不返回
return false;
}
for(int i = 2; i <= n; i++)
if(!used[i] && prime[i + ans[cnt - 1]] == 1){
used[i] = true;
ans[cnt] = i;
if(dfs(cnt + 1))
return true;
used[i] = false;
}
return false;
}
int main(){
cin.tie(0);
cout.tie(0);
int Case = 0;
Prime();
while(~scanf("%d", &n)){
Case++;
cout << "Case " << Case << ':' << endl;
memset(used, 0, sizeof(used));
//Note: the number of first circle should always be 1.
ans[0] = 1;
used[1] = true;
dfs(1);
cout << endl;
}
}
hdu1016
最新推荐文章于 2019-05-31 12:34:01 发布