| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 776 人关注过本帖
标题:魔方阵 C代码
只看楼主 加入收藏
hejing1109
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-27
收藏
 问题点数:0 回复次数:5 
魔方阵 C代码

齐数阶魔方阵

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 20

void main(void)
{
int matrix[MAXSIZE][MAXSIZE]; /* the magic square */
int count; /* 1..n*n counting */
int row; /* row index */
int column; /* column index */
int order; /* input order */
char line[100];

printf("\nOdd Order Magic Square Generator");
printf("\n================================");
printf("\n\nOrder Please --> ");
gets(line);
order = atoi(line);

if (order > MAXSIZE)
printf("\n*** ERROR *** Order should be <= %d", MAXSIZE);
else if (order % 2 == 0)
printf("\n*** ERROR *** Order must be an odd integer");
else {
row = 0; /* start of from the middle */
column = order/2; /* of the first row. */
for (count = 1; count <= order*order; count++) {
matrix[row][column] = count; /* put next # */
if (count % order == 0) /* move down ? */
row++; /* YES, move down one row */
else { /* compute next indices */
row = (row == 0) ? order - 1 : row - 1;
column = (column == order-1) ? 0 : column + 1;
}
}
printf("\n\nMagic Square of order %d :\n\n", order);
for (row = 0; row < order; row++) {
for (column = 0; column < order; column++)
printf("%4d", matrix[row][column]);
printf("\n");
}
}
}

搜索更多相关主题的帖子: 魔方 代码 
2006-11-16 04:05
hejing1109
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-27
收藏
得分:0 

单偶数阶魔方阵
#define MAXSIZE 30

void singly_even(int [][MAXSIZE], int);
void magic_o(int [][MAXSIZE], int);
void exchange(int [][MAXSIZE], int);

/* ------------------------------------------------------ */
/* FUNCTION singly_even : */
/* This is the driver program. It fills the upper-left*/
/* lower-right, upper-right and lower-left parts by using */
/* odd order magic square routine. Then exchange some */
/* parts in each routine in order to maintain the magic */
/* properties. */
/* ------------------------------------------------------ */

void singly_even(int matrix[][MAXSIZE], int n)
{
int half = n/2;

magic_o(matrix, half);
exchange(matrix, n);
}


/* ------------------------------------------------------ */
/* FUNCTION magic_o : */
/* Odd order magic square routine. It fills the block */
/* bounded by left, right, top and bottom with numbers */
/* starting from 'start'. Otherwise all are the same as */
/* the magic square routine discussed before. */
/* ------------------------------------------------------ */

void magic_o(int matrix[][MAXSIZE], int n)
{
int count; /* fill counting */
int row; /* row index */
int column; /* column index */

row = 0; /* start of from the middle */
column = n/2; /* of the first row. */
for (count = 1; count <= n*n; count++) {
matrix[row][column] = count; /* put # in A */
matrix[row+n][column+n] = count + n*n; /* in B */
matrix[row][column+n] = count + 2*n*n; /* in C */
matrix[row+n][column] = count + 3*n*n; /* in D */
if (count % n == 0) /* move downward ? */
row++; /* YES, move down one row */
else { /* compute next indices */
row = (row == 0) ? n - 1 : row - 1;
column = (column == n-1) ? 0 : column + 1;
}
}
}

#define SWAP(x,y) { int t; t = x; x = y; y = t;}

void exchange(int x[][MAXSIZE], int n)
{
int width = n / 4;
int width1 = width - 1;
int i, j;

for (i = 0; i < n/2; i++)
if (i != width) { /* if not the width-row */
for (j = 0; j < width; j++)
SWAP(x[i][j], x[n/2+i][j]);
for (j = 0; j < width1; j++)
SWAP(x[i][n-1-j], x[n/2+i][n-1-j]);
}
else { /* width-row is special */
for (j = 1; j <= width; j++)
SWAP(x[width][j], x[n/2+width][j]);
for (j = 0; j < width1; j++)
SWAP(x[width][n-1-j], x[n/2+width][n-1-j]);
}
}

/* ------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h>

void main(void)
{
int matrix[MAXSIZE][MAXSIZE];
int n;
int i, j;
char line[100];

printf("\nSingly-Even Order Magic Square");
printf("\n==============================");
printf("\n\nOrder Please (must be 2*(2k+1)) --> ");
gets(line);
n = atoi(line);

if (n % 2 == 0 && (n/2) % 2 == 1) {
singly_even(matrix, n);
printf("\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%4d", matrix[i][j]);
printf("\n");
}
}
else
printf("\n*** Illegal Order ***");
}


2006-11-16 04:06
hejing1109
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-27
收藏
得分:0 

双偶数阶魔方阵

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 20 /* square size */
#define MARK -1 /* marker for build up */

void main(void)
{
int square[MAXSIZE][MAXSIZE]; /* the square */
int n; /* order of the magic square*/
int count, inv_count; /* 1 -> n^2 and n^2 -> 1 */
int marker; /* working marker 1,-1,1,-1 */
int i, j;
char line[100];

printf("\nDoubly-Even Magic Square");
printf("\n========================");
printf("\n\nOrder (4*m, m>0) please --> ");
gets(line);
n = atoi(line);
if (n % 4 != 0)
printf("\n*** Illegal Order *****");
else {
marker = MARK; /* mark the upper part */
for (i = 0; i < n/2; i++, marker = -marker)
for (j = 0; j < n/2; j++, marker = -marker)
square[i][j] = square[i][n-1-j] = marker;

count = 1; /* upward counter */
inv_count = n*n; /* downward counter */
for (i = 0; i < n/2; i++)
for (j = 0; j < n; j++)
if (square[i][j] != MARK) { /* marked*/
square[i][j] = count++;
square[n-1-i][n-1-j] = inv_count--;
}
else { /* unmarked */
square[i][j] = inv_count--;
square[n-1-i][n-1-j] = count++;
}
printf("\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
printf("%4d", square[i][j]);
printf("\n");
}
}
}


2006-11-16 04:09
梦幻情缘
Rank: 6Rank: 6
等 级:贵宾
威 望:29
帖 子:769
专家分:20
注 册:2005-4-4
收藏
得分:0 
不错呀
2006-11-16 12:59
卧龙孔明
Rank: 9Rank: 9Rank: 9
等 级:贵宾
威 望:59
帖 子:3872
专家分:684
注 册:2006-10-13
收藏
得分:0 
鼎鼎...顶!

My Blog: www.aiexp.info
虽然我的路是从这里开始的,但是这里不再是乐土.感谢曾经影响过,引导过,帮助过我的董凯,飞燕,leeco,starwing,Rockcarry,soft_wind等等等等.别了,BCCN.
2006-11-16 18:14
guzhou
Rank: 1
等 级:新手上路
威 望:1
帖 子:247
专家分:0
注 册:2006-11-4
收藏
得分:0 
写得真好!

2006-11-17 17:27
快速回复:魔方阵 C代码
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.028152 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved