题目大意:
http://codevs.cn/problem/1031/
#include <iostream> #include <cmath> #include <algorithm> #include <string> using namespace std; int arr[17] = {0}; bool f[17] = {false}; bool isPrime(int a) { if(a == 0) return false; if(a == 1) return true; for(int i = 2; i <= sqrt(a); i++) { if(a % i == 0) return false; } return true; } void dfs(int a, int n) { if(a == n && isPrime(arr[n-1] + arr[0])) { for(int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return; } for(int i = 2; i <= n; i++) { if(!f[i] && isPrime(i + arr[a-1])) { arr[a] = i; f[i] = true; dfs(a+1,n); f[i] = false; } } } int main() { int n; cin >> n; arr[0] = 1; f[0] = true; dfs(1,n); return 0; }