Wa了,求助!!
查看原帖
Wa了,求助!!
519573
Daniel_yao楼主2022/3/8 22:18
#include<bits/stdc++.h>
using namespace std;
const int N = 20;
int n;
int a[N], num;
bool f[N];

bool zhi(int x){
	if(x < 2){
		return 0;
	}
	for(int i = 2;i * i <= x; i++){
		if(x % i == 0){
			return 0;
		}
	}
	return 1;
}

void print(){
	for(int i = 1;i <= n;i++){
		cout << a[i] << " ";
	}
	cout << endl;
}

void dfs(int cnt, int last){
	if(cnt == n + 1){
		if(zhi(a[n] + a[1]) == 1){
			print();
			return ;
		}
		else{
			return ;
		}
	}
	for(int i = 2;i <= n;i++){
		if(!f[i] && zhi(last + i) == 1 || cnt == 1){
			f[i] = 1;
			a[cnt] = i;
			dfs(cnt + 1, a[cnt]);
			f[i] = 0;
			a[cnt] = 0;
		}
	}
}

int main(){
	int i = 1;
	while(scanf("%d",&n)!=EOF){
		if(i >= 2){
			cout << endl;
		}
		a[1] = 1;
		num++;
		cout << "Case " << num << ":" << endl;
		dfs(2, 1);
		i++;
	}
	return 0;
}
2022/3/8 22:18
加载中...