---
title: "四阶幻方"
author: "t0uch"
date: "2007-08-23T15:59:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2007-08-23T15:59:00Z"
id: 115681
url: "https://t0uch.iteye.com/blog/115681"
---

我写的四阶幻方出来了，运行结果正确，7040个的版本。虽然好像还有些问题，不过算了，能跑就可以了，呵呵。  
不要输出的话我的p4 3.2G上耗时5s，算还过得去吧 ^\_^  
以下是程序：

cpp 代码

 

1. #include &lt;iostream&gt;&lt;/iostream&gt;
   
   #include &lt;time.h&gt;&lt;/time.h&gt;
   
   using namespace std;  
   
     
   
   struct uni  
   
   {  
   
       int a,b,c,d;  
   
   };  
   
     
   
   uni can\[2065];  
   
   long sum = 0;  
   
   int sum\_all = 0;  
   
     
   
   void get\_all\_num()  
   
   {  
   
       for(int i = 1;i &lt;= 16;++i)  
   
       {  
   
           for(int j = 1;j &lt;= 16;++j)  
   
           {  
   
               for(int k = 1; k &lt;= 16; ++k)  
   
               {  
   
                   for(int l = 1;l &lt;= 16;++l)  
   
                   {                 
   
                       if(i != j && i != k && i != l && j != k && j != l && k != l)  
   
                           if(i + j + k + l == 34)  
   
                           {  
   
                               can\[sum].a = i;  
   
                               can\[sum].b = j;  
   
                               can\[sum].c = k;  
   
                               can\[sum].d = l;  
   
                               ++sum;  
   
                           }  
   
                   }  
   
               }  
   
           }  
   
       }  
   
   }  
   
     
   
   void p\_rint(const int sq\[])  
   
   {  
   
       cout &lt;&lt; "i wIll gIvE yOu sOme coLoR 2CC! " &lt;&lt; sum\_all &lt;
   
       cout &lt;&lt; sq\[0] &lt;&lt; " " &lt;&lt; sq\[1] &lt;&lt; " " &lt;&lt; sq\[2] &lt;&lt; " " &lt;&lt; sq\[3] &lt;&lt; endl;  
   
       cout &lt;&lt; sq\[4] &lt;&lt; " " &lt;&lt; sq\[5] &lt;&lt; " " &lt;&lt; sq\[6] &lt;&lt; " " &lt;&lt; sq\[7] &lt;&lt; endl;  
   
       cout &lt;&lt; sq\[8] &lt;&lt; " " &lt;&lt; sq\[9] &lt;&lt; " " &lt;&lt; sq\[10] &lt;&lt; " " &lt;&lt; sq\[11] &lt;&lt; endl;  
   
       cout &lt;&lt; sq\[12] &lt;&lt; " " &lt;&lt; sq\[13] &lt;&lt; " " &lt;&lt; sq\[14] &lt;&lt; " " &lt;&lt; sq\[15] &lt;&lt; endl &lt;&lt; endl;  
   
   }  
   
     
   
   inline bool caca(const int sq\[])  
   
   {  
   
       if(sq\[0] + sq\[4] + sq\[8] + sq\[12] !=34)  
   
           return false;  
   
       if(sq\[2] + sq\[6] + sq\[10] + sq\[14] !=34)  
   
           return false;  
   
       return true;  
   
   }  
   
     
   
   inline bool detec\_2(const int sq\[])  
   
   {  
   
       int km2\[17];  
   
       int i,m;  
   
       for(i = 0;i &lt; 16;++i)  
   
       {  
   
           km2\[i] = 0;  
   
       }  
   
     
   
       for(m = 0;m &lt; 16;++m)  
   
       {  
   
           km2\[sq\[m]]++;  
   
           if(km2\[sq\[m]] &gt; 1)  
   
               return false;  
   
       }  
   
     
   
       return true;  
   
   }  
   
   inline bool detec\_1(const int c1\[],const int c2\[])  
   
   {  
   
       int km2\[17];  
   
       for(int i = 0;i &lt; 16;++i)  
   
       {  
   
           km2\[i] = 0;  
   
       }  
   
     
   
       km2\[c1\[0] - 1]++;  
   
       km2\[c1\[1] - 1]++;  
   
       km2\[c1\[2] - 1]++;  
   
       km2\[c1\[3] - 1]++;  
   
         
   
       km2\[c2\[0] - 1]++;  
   
       km2\[c2\[1] - 1]++;  
   
       km2\[c2\[2] - 1]++;  
   
       km2\[c2\[3] - 1]++;  
   
     
   
       for(i = 0;i &lt; 16;i++)  
   
       {  
   
           if(km2\[i] &gt; 1)  
   
               return false;  
   
       }  
   
     
   
       return true;  
   
   }  
   
     
   
   void fill\_in\_line(int c1\[],int c2\[])  
   
   {  
   
       int sq\[16];  
   
     
   
       sq\[0] = c1\[0];  
   
       sq\[5] = c1\[1];  
   
       sq\[10] = c1\[2];  
   
       sq\[15] = c1\[3];  
   
     
   
       sq\[3] = c2\[0];  
   
       sq\[6] = c2\[1];  
   
       sq\[9] = c2\[2];  
   
       sq\[12] = c2\[3];           
   
     
   
       for(int k = 1;k &lt;= 16;k++)  
   
       {  
   
           sq\[4] = k;  
   
             
   
           for(int l = 1;l &lt;= 16;++l)  
   
           {  
   
               sq\[1] = l;  
   
                     
   
               sq\[7] = 34 - sq\[4] - sq\[5] - sq\[6];  
   
               sq\[11] = 34 - sq\[3] - sq\[7] - sq\[15];  
   
               sq\[8] = 34 - sq\[9] - sq\[10] - sq\[11];  
   
               sq\[13] = 34 - sq\[1] - sq\[5] - sq\[9];  
   
               sq\[14] = 34 - sq\[12] - sq\[13] - sq\[15];  
   
               sq\[2] = 34 - sq\[0] - sq\[1] - sq\[3];  
   
             
   
               if(sq\[7] &gt; 0 && sq\[11] &gt; 0 && sq\[8] &gt; 0 && sq\[13] &gt; 0 && sq\[14] &gt; 0 && sq\[2] &gt; 0)  
   
                   if(sq\[7] &lt;= 16 && sq\[11] &lt;= 16 && sq\[8] &lt;= 16 && sq\[13] &lt;= 16 && sq\[14] &lt;= 16 && sq\[2] &lt;= 16)  
   
                       if(detec\_2(sq) && caca(sq))  
   
                       {  
   
                           sum\_all++;  
   
                           p\_rint(sq);  
   
                       }  
   
           }  
   
       }  
   
     
   
   }  
   
   void fill\_in\_eight()  
   
   {  
   
       int cse1\[4],cse2\[4];  
   
     
   
       for(int i = 0;i &lt; sum;++i)  
   
       {  
   
           for(int j = 0;j &lt; sum;++j)  
   
           {  
   
               if( i!=j )  
   
               {  
   
                   cse1\[0] = can\[i].a;  
   
                   cse1\[1] = can\[i].b;  
   
                   cse1\[2] = can\[i].c;  
   
                   cse1\[3] = can\[i].d;  
   
     
   
                   cse2\[0] = can\[j].a;  
   
                   cse2\[1] = can\[j].b;  
   
                   cse2\[2] = can\[j].c;  
   
                   cse2\[3] = can\[j].d;  
   
                     
   
                   if(detec\_1(cse1,cse2))  
   
                   {  
   
                       fill\_in\_line(cse1,cse2);  
   
                   }  
   
               }  
   
                 
   
           }  
   
       }  
   
   }  
   
     
   
   int main()  
   
   {  
   
       time\_t t;  
   
       t = clock();  
   
     
   
       get\_all\_num();  
   
       fill\_in\_eight();  
   
     
   
       cout &lt;&lt; "time consumed: " &lt;&lt; clock() - t &lt;&lt; " ms"&lt;&lt; endl;  
   
       cin.get();  
   
       return 0;  
   
   }

写得比较烂，咳咳咳

找个时间，来个ruby版的。  
ps：最近发现牛人们都很臭屁，臭不可闻啊，唉，风气啊风气。做技术的人都很臭屁，果然啊果然，但愿我以后不会这样。