Hex Magic square C language

Source: Internet
Author: User

The method may be a bit stupid, take the first outer ring (1-12), and then the Middle (13-18) of the sorting method, so that the first pruning as far as possible to cut off three sums, and then remove four sum.

But with arrays only, checking for repetition seems like a difficult thing to do, and the 12 results are actually one, 2 (mirror) *6 (angle)

Original code, lhy1024

#include <stdio.h> #include <time.h> #define SUM 38int count=0;int Check (int a[],int flag), int arrange (int a[] , int ordinal), void magic_printf (int a[]), int main () {int a[20]= {0};d ouble start,finish; Start=clock ();//Timing part arrange (A, 1); Finish=clock ();p rintf ("%f seconds\n", (Finish-start)/clocks_per_sec); return 0;} int arrange (int a[],int ordinal) {int i,j;if (ordinal==20) {count++;magic_printf (a);//last one to print}else {int b[20]= { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};for (i=1; i<=ordinal-1; i++) {b[a[i]]=0;//used numbers turn the switch to zero}for (i= 1; i<=19; i++) {if (b[i]!=0) {a[ordinal]=i;if (check (a,ordinal) ==1)//If eligible, recursively next arrange (a,ordinal+1);}}} int check (int a[],int flag) {if (flag==3| | flag==5| | flag==7| | flag==9| | flag==11) {if (a[flag-2]+a[flag-1]+a[flag]!=sum) return 0;} if (flag==14) {if (a[12]+a[13]+a[14]+a[4]!=sum) return 0;} if (flag==15) {if (a[2]+a[14]+a[15]+a[6]!=sum) return 0;} if (flag==16) {if (a[4]+a[15]+a[16]+a[8]!=sum) return 0;} if (flag==17) {if (a[10]+a[17]+a[16]+a[6]!=sum) return 0;} If(flag==18) {if (a[8]+a[17]+a[18]+a[12]!=sum) return 0;if (A[2]+a[13]+a[18]+a[10]!=sum) return 0;} if (flag==19) {if (a[1]+a[13]+a[19]+a[16]+a[7]!=sum) return 0;if (a[3]+a[14]+a[19]+a[17]+a[9]!=sum) return 0;if (a[11]+ A[18]+a[19]+a[15]+a[5]!=sum) return 0;} return 1;} void magic_printf (int a[]) {printf ("Case:%d\n", Count);p rintf ("%2d%2d%2d\n", a[1],a[2],a[3]);p rintf ("%2d%2d%2 D%2d\n ", A[12],a[13],a[14],a[4]);p rintf ("%2d%2d%2d%2d%2d\n ", a[11],a[18],a[19],a[15],a[5]);p rintf ("%2d%2d%2d %2d\n ", a[10],a[17],a[16],a[6]);p rintf ("%2d%2d%2d\n ", a[9],a[8],a[7]);p rintf (" \ n ");}

Hex Magic square C language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.