标签:
#include <iostream> #include <stdlib.h> #include <math.h> using namespace std; #include <stdio.h> #include <math.h> #define LEN 20 int primeRing(int ring[], int b[], int n); int isPrime(int n); int main(void) { int i, ring[LEN]={0}, b[LEN]={0}; ring[0] = 1; b[0] = 1; if( primeRing(ring, b, 1) ) { //printf("\n\nThe prime ring is : "); for(i=0; i<LEN; i++) printf("%d ", ring[i]); //printf("\n"); } else printf("\n\nNot found!\n"); return 0; } int primeRing(int ring[], int b[], int n) { int i; if( n==LEN ) return isPrime(ring[n-1]+ring[0]); //printf("\nCalculating ring[%d] = ", n); for(i=1; i<LEN; i++) if( b[i]==0 && isPrime((i+1)+ring[n-1]) ) { b[i] = 1; //cout<<" "<<i+1<<endl; ring[n] = i+1; //printf("%d ", ring[n]); if( primeRing(ring, b, n+1) ) return 1; else b[i] = 0; } return 0; } int isPrime(int n) { int i, t, f=1; t = sqrt(n); for(i=2; i<=t; i++) if( n%i==0 ) { f = 0; break; } return f; }
代码来自:http://blog.sina.com.cn/s/blog_52e9ba5f0100a1y9.html
标签:
原文地址:http://www.cnblogs.com/lxk2010012997/p/4379947.html