站外题求助!
  • 板块灌水区
  • 楼主China_19491001
  • 当前回复11
  • 已保存回复11
  • 发布时间2024/8/9 11:29
  • 上次更新2024/8/9 13:00:09
查看原帖
站外题求助!
1312025
China_19491001楼主2024/8/9 11:29

题目

错误方式:RE

代码:

#include<bits/stdc++.h>
using namespace std;
int search(int);
void print();
bool pd(int,int);
int n;
int a[888];
bool b[888];
int total=0;
int main(){
	cin>>n;
	search(1);
	return 0;
}
int search(int t){
	for(int i=1;i<=n;i++){
		if((pd(a[t-1],i))&&(!b[i])){
			a[t]=i;
			b[i]=1;
			if(t==n){if(pd(a[n],a[1])) print();}
			search(t+1);
			b[i]=false;
		}
	}
}
void print(){
	if(total==1) return ;
	++total;
	for(int j=1;j<=n;j++){
		cout<<a[j]<<" ";
	}
	cout<<endl;
}
bool pd(int a,int b){
	int d=2,c=a+b;
	while(d<=sqrt(c)&&c%d!=0) d++;
	if(d>sqrt(c)) return true;
	else return false;
}

求助!!!

2024/8/9 11:29
加载中...