算法设计与分析:第五章 回溯法 5.4素数环

来源:互联网 发布:js中json转数组 编辑:程序博客网 时间:2024/05/22 10:22
/*素数环:步骤1初始化2递归填数输入:68输出case11 4 3 2 5 61 6 5 2 3 4case21 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2*/#include <iostream>using namespace std;bool isRepeat(int k , int pos, int* x){for(int i = 1 ; i < pos ; i++){//如果前面的数与当前数字相同,待比较的数字是kif(x[i] == k){return true;}}return false;}bool isPrime(int i){int k = (int)sqrt(i);for(int j = 2 ; j <= k ; j++){if(i % j == 0){return false;}}return true;}bool isSumPrime(int pos , int* x , int n , int k){//如果没有达到n,只需要正常检查if(pos < n){//直接用k进行尝试,而不是x[k]if(isPrime(k + x[pos - 1])){return true;}}else{//还要检查第一个和最后一个if(isPrime(k + x[pos - 1]) && isPrime(k + x[1])){return true;}}return false;}void primeCircle(int pos , int n , int& iCount , int* x){//对2~20号数字进行尝试for(int k = 2 ; k <= n ; k++){//检查数字是否重复,判断相邻和是否为素数if(!isRepeat(k , pos , x) && isSumPrime(pos, x , n , k)){x[pos] = k;if(pos == n){for(int p = 1 ; p <= n ; p++){cout << x[p] << "\t" ;}cout << endl;iCount++;}//尝试下一个数else{primeCircle(pos+1 , n ,iCount , x);//回溯,清理现场。但是这里并没有做x[pos] = 0;}}}}void process(){int n ;int x[100];while( cin >> n){int iCount = 0;int pos = 2;memset(x, 0 , sizeof(x));x[1] = 1;//初始化memset(mark , 0 , sizeof(mark));//初始化访问标记mark[1] = true;primeCircle(pos , n , iCount , x);cout << iCount << endl;}}int main(int argc, char* argv[]){process();getchar();return 0;}

0 0
原创粉丝点击