OJ-Problems-Source/CodeVS/1031_冰封.c

50 lines
664 B
C
Raw Normal View History

2016-07-06 14:28:18 +08:00
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD>:ǧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
<EFBFBD><EFBFBD>Ŀ:p1031 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
#include <stdio.h>
#include <math.h>
int a[17];
int book[18];
int size;
int isPrime(int num){
int i;
for(i = 2; i <= sqrt(num); i++)
if(!(num % i))
return 0;
return 1;
}
void dfs(int idx){
if(idx == size){
if(!isPrime(a[0] + a[idx])) return ;
int i;
printf("1");
for(i = 1; i < size; i++)
printf(" %i", a[i]);
printf("\n");
return ;
}
int i;
for(i = 2; i <= size; i++){
if(!book[i] && isPrime(a[idx - 1] + i)){
book[i] = 1;
a[idx] = i;
dfs(idx + 1);
book[i] = 0;
}
}
}
int main() {
scanf("%i", &size);
a[0] = 1;
book[0] = 1;
book[1] = 1;
dfs(1);
return 0;
}