程序代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #define N 4 #define Max 100 int buf[N]={0}; int array[Max] = { 3 }; int output[3] = {}; int store[24] ={}; bool deepCheck() { int subbuf[3] = {store[output[0]] , store[output[1]], store[output[2]]}; std::sort(subbuf,subbuf+3); if ( subbuf[1] - subbuf[0] == subbuf[2] - subbuf[1] ) { return true; } else return false; } void comb3( int m , int n=3 ) { for ( int i = m ; i >= n ; i -- ) { array[n] = i; if ( n > 1 ) comb3(i -1 , n -1 ); else { for ( int j = array[0]; j > 0 ; j -- ) { output[j]=array[j] ; //std::cout<<output[j]<<" "; } //std::cout<<std::endl; if (deepCheck()) { std::cout<<store[output[0]]<<" "<<store[output[1]]<<" "<<store[output[2]]<<std::endl; } } } } bool isOk(int k) { if ( k == 0 ) return true; else if ( buf[k] > buf[ k-1] ) return true; else return false; } bool isPrime(int n) { if( n == 0 || n == 1) return false; if ( n == 2 ) return true; for ( int i =2 ; i < n/2; i ++ ) { if ( n % i == 0 ) return false; } return true; } int buf2Num(int * buffer) { int ret = 0; for( int i = 0 ; i < N ; i++) { ret *= 10; ret += buffer[i]; } return ret; } void checkSub() { int buffer[4] = { 0 }; int k = 0 ; memcpy(buffer, buf,4*sizeof( int )); do{ int num = buf2Num(buffer); if( isPrime(num) && num > 1000) store[k++] = num; }while( std::next_permutation(buffer+0,buffer+4)); /* for( int i =0 ; i < k ; i ++ ) { std::cout<<store[i]<<" "; } std::cout<<std::endl; */ comb3(k); } void recurse(int n) { if ( n == N) { // for ( int i = 0 ; i < N ; i ++ ) // { // std::cout<<buf[i]<<" "; // } // std::cout<<std::endl; checkSub(); } else { for( int j = 0 ; j < 10; j ++ ) { buf[n] = j ; if ( isOk(n)) recurse(n+1) ; } } } int main() { recurse(0); }