欢迎来到一句话经典语录网
我要投稿 投诉建议
当前位置:一句话经典语录 > 读后感 > 幻方与素数读后感

幻方与素数读后感

时间:2019-10-25 08:41

求素数幻方

急啊、、、

素数幻方源程序:#include#inclued#include#define MAX_NUM 30#define _PRINT_0unsigned long Result[MAX_NUM * MAX_NUM], ResultNum, Used[MAX_NUM * MAX_NUM]={0};bool PrimeTable[MAX_NUM * MAX_NUM * 2]={ false }; unsigned long N, QN; void CreatePrimeTable(void) { PrimeTable[0]=false; PrimeTable[1]=false; PrimeTable[2]=true; PrimeTable[3]=true; for(unsigned long j=5; j <= MAX_NUM * MAX_NUM * 2; j+=2) { PrimeTable[j]=true; for(unsigned long i=3; i <= sqrt((double)j); i+=2) { if(j % i == 0) { PrimeTable[j]=false; break; } } } } inline bool IsPrime(unsigned long n) { return PrimeTable[n]; } bool CheckIt(unsigned long Deep) { if(Deep == 0) { return true; } else if(Deep < N) { return IsPrime(Result[Deep] + Result[Deep - 1]); } else if(Deep % N == 0) { return IsPrime(Result[Deep] + Result[Deep - N]); } else { return(IsPrime(Result[Deep] + Result[Deep - 1]) && IsPrime(Result[Deep] + Result[Deep - N])); } } void go(unsigned long Deep) { if(Deep == QN) { ResultNum++; #if (_PRINT_) printf(Find it! No.%lu\\\ , ResultNum); for(unsigned long i=0; i < QN; i++) { printf(%lu\\\, Result[i]); if(i % N == N - 1) { printf(\\\ ); } } #else printf(\\\\rFind:%lu, ResultNum); #endif } else { for(unsigned long i=1; i <= QN; ++i) { if(!Used[i]) { Result[Deep]=i; if(CheckIt(Deep)) { Used[i]=1; go(Deep + 1); Used[i]=0; } } } } } int main() { DWORD tim; ResultNum=0; printf(Input N:); scanf(%lu, &N); QN=N * N; tim=GetTickCount(); CreatePrimeTable(); go(0); printf(\\\ \\\ N=%lu\\\ , N); printf(Total=%lu\\\ , ResultNum); printf(Time=%lu\\\ , GetTickCount() - tim); return 0; }

声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。联系xxxxxxxx.com

Copyright©2020 一句话经典语录 www.yiyyy.com 版权所有

友情链接

心理测试 图片大全 壁纸图片