打印"魔方阵".魔方阵就是这样的方正,他的每一行,每一列和对角线之和相等,比如3阶的就是
8 1 6
3 5 7
4 9 2
要求打印N阶魔方阵,就是1-n平方构成的方正
任意阶魔方阵的源代码
特别说明:因此源码非我所写,所以收费高了点
[UseMoney=200]
#include <stdio.h> #define N 100 unsigned matrix[N][N]; void init(void); void odd(unsigned startdigit,unsigned char startrow,unsigned char startcol,unsigned char rowcol); void odd_even(unsigned char rowcol); void even_even(unsigned char rowcol); void print(unsigned char rowcol); void verify(unsigned char rowcol); main() { unsigned char number; printf("Please input a number:"); scanf("%d",&number); init(); if (number % 2) odd(1,0,0,number); else if (number % 4) odd_even(number); else even_even(number); print(number); verify(number); return 0; } void init(void) { unsigned char i,j; for (i=0;i<N;i++) for (j=0;j<N;j++) matrix[i][j] = 0 ; } void odd(unsigned startdigit,unsigned char startrow,unsigned char startcol,unsigned char rowcol) { unsigned char row,col,row0,col0; unsigned i; row0 = row = startrow; col = (rowcol - 1)/2 + startcol; matrix[row][col]=startdigit; for (i=startdigit+1;i<=startdigit+rowcol*rowcol-1;i++) { if (row-1 < startrow) row = startrow+rowcol-1; else row--; if (col+1 > startcol+rowcol-1) col = startcol; else col++; if (!matrix[row][col]) { row0=row; col0=col; matrix[row][col]=i; } else { row=row0; if (row+1 > startrow+rowcol-1) row = startrow; else row++; col=col0; matrix[row][col]=i; } } } void odd_even(unsigned char rowcol) { unsigned char k=rowcol/2; unsigned char row,col; unsigned t,l; odd(1,0,0,k); odd(k*k+1,k,k,k); odd(2*k*k+1,0,k,k); odd(3*k*k+1,k,0,k); for (col=0;col<=k/2-1;col++) for (row=0;row<k;row++) if (!(col == 0 && row+k == k/2+k)) { t = matrix[row+k][col]; matrix[row+k][col] = matrix[row][col]; matrix[row][col] = t; } t = matrix[(k-1)/2+k][(k-1)/2]; matrix[(k-1)/2+k][(k-1)/2] = matrix[(k-1)/2][(k-1)/2]; matrix[(k-1)/2][(k-1)/2] = t; l = (k-1)/2; for (col=0;col<l-1;col++) for (row=k;row<rowcol;row++) { t = matrix[row][rowcol-col-1]; matrix[row][rowcol-col-1] = matrix[row-k][rowcol-col-1]; matrix[row-k][rowcol-col-1] = t; } } void even_even(unsigned char rowcol) { unsigned order1=rowcol*rowcol,order2=1; unsigned char row,col,k; for (row=0;row<rowcol;row++) for (col=0;col<rowcol;col++) if ((row % 4) == (col % 4) || ((row % 4) + (col % 4) == 3)) { matrix[row][col] = order1--; order2++; } else { matrix[row][col] = order2++; order1--; } } void print(unsigned char rowcol) { char *str="\xc4\xc4\xc4\xc4"; unsigned char i,j; for (i=0;i<rowcol;i++) { for (j=0;j<rowcol;j++) { if (i == 0 ) if (j == 0) printf("\xda"); else printf("\xc2"); else if (j == 0) printf("\xc3"); else printf("\xc5"); printf("%s",str); } if (i == 0) printf("\xbf\n"); else printf("\xb4\n"); for (j=0;j<rowcol;j++) printf("\xb3%4d",matrix[i][j]); printf("\xb3\n"); } printf("\xc0%s",str); for (j=1;j<rowcol;j++) printf("\xc1%s",str); printf("\xd9\n"); } void verify(unsigned char rowcol) { unsigned char i,j; unsigned sum=0,sum1=0; for (i=0;i<rowcol;i++) { sum1=0; for (j=0;j<rowcol;j++) sum1+=matrix[i][j]; if (sum == 0) sum=sum1; else if (sum1 != sum) { printf("Error!\n"); return; } } for (i=0;i<rowcol;i++) { sum1=0; for (j=0;j<rowcol;j++) sum1+=matrix[j][i]; if (sum == 0) sum=sum1; else if (sum1 != sum) { printf("Error!\n"); return; } } //1+2+3+ ... +n2=n2(n2+1)/2 printf("Verify OK! The sum is %d\n",sum);//sum= n*n*(n*n+1)/2 }
[/UseMoney]
谁说只有奇数的时候才行?
Please input a number:4 ┌────┬────┬────┬────┐ │ 16│ 2│ 3│ 13│ ├────┼────┼────┼────┤ │ 5│ 11│ 10│ 8│ ├────┼────┼────┼────┤ │ 9│ 7│ 6│ 12│ ├────┼────┼────┼────┤ │ 4│ 14│ 15│ 1│ └────┴────┴────┴────┘ Verify OK! The sum is 34
Please input a number:10 ┌────┬────┬────┬────┬────┬────┬────┬────┬────┬────┐ │ 92│ 99│ 1│ 8│ 15│ 67│ 74│ 51│ 58│ 40│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 98│ 80│ 7│ 14│ 16│ 73│ 55│ 57│ 64│ 41│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 4│ 81│ 88│ 20│ 22│ 54│ 56│ 63│ 70│ 47│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 85│ 87│ 19│ 21│ 3│ 60│ 62│ 69│ 71│ 28│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 86│ 93│ 25│ 2│ 9│ 61│ 68│ 75│ 52│ 34│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 17│ 24│ 76│ 83│ 90│ 42│ 49│ 26│ 33│ 65│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 23│ 5│ 82│ 89│ 91│ 48│ 30│ 32│ 39│ 66│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 79│ 6│ 13│ 95│ 97│ 29│ 31│ 38│ 45│ 72│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 10│ 12│ 94│ 96│ 78│ 35│ 37│ 44│ 46│ 53│ ├────┼────┼────┼────┼────┼────┼────┼────┼────┼────┤ │ 11│ 18│ 100│ 77│ 84│ 36│ 43│ 50│ 27│ 59│ └────┴────┴────┴────┴────┴────┴────┴────┴────┴────┘ Verify OK! The sum is 505
还是花点小钱看看,不多啊?才200
[此贴子已经被作者于2004-08-14 23:29:02编辑过]