求助
查看原帖
求助
941334
zhangyong楼主2024/10/24 18:22
#include<bits/stdc++.h>
using namespace std;
int n,vis[20]={0,1},a[20]={0,1},cnt;
bool judg(int x){
	//最小2个数的和为3,所以不需要特殊判断
	for(int i=2;i<=sqrt(x);i++){
		if(x % i == 0){
			return false;
		}
	} 
	return true;
}
void dfs(int k){
	if(k>n && judg(a[n]+a[1])){
		for(int i=1;i<=n;i++){
			if(i!=n) cout << a[i] << " ";
			else cout << a[i];
		}
		cout << endl;
		return;
	}
	for(int i=2;i<=n;i++){
		if(vis[i] == 0){
			if(judg(i+a[k-1])){
				vis[i] = 1;
				a[k] = i;
				dfs(k+1);
				vis[i] = 0;
			}
		}
	}
}
int main(){
	while(cin >> n){
		cout << "Case " << ++cnt << ":" << endl;
		dfs(2);
		cout << endl;
	}
	return 0;
}

2024/10/24 18:22
加载中...