c语言方阵填数程序设计论文,求助,有哪位大神可以帮我解决方阵填数问题

已结贴√

问题点数:20 回复次数:3

ca56232b3bbedf9a539d07f37fffb99a.gif

3144d8b7615c79d9f638db40d5689d26.gif

a218af6549b45ee526caf607ebff1358.gif

0f8df0e29816ae721419de940fb833d1.gif

求助,有哪位大神可以帮我解决方阵填数问题

我的目的是为了算出一个方阵:

1  2  3  4  5   6  7  8  9  10

36 37 38 39 40  41 42 43 44 11

35 64 65 66 67  68 69 70 45 12

34 63 84 85 86  87 88 71 46 13

33 62 83 96 97  98 89 72 47 14

31 61 82 95 100 99 90 73 48 15

32 60 87 94 93  92 91 74 49 16

30 59 80 79 78  77 76 75 50 17

29 58 57 56 55  54 53 52 51 18

28 27 26 25 24  23 22 21 20 19

请问哪里错了,我写的代码为:

#include

#include

#define N 10

enum DIRECTIONS {DOWN=0, RIGHT=1, UP=2, LEFT=3}; //4个方向

int gMtrx[N][N]; //固定大小的方阵

/*【功能】打印输出方阵

【输入】int *mtrx:将方阵看成一个一维数组所对应的数组首地址

int n:方阵的行数

【输出】无*/

void printMtrx(int *mtrx, int n)

{

int i, j;

for(i=0; i

{

for(j=0;j

{

printf("%5d  ", *(mtrx+i*n+j));

}

printf("\n");

}

}

/*【功能】按方式A填充矩阵

【输入】int *mtrx:将方阵看成一个一维数组所对应的数组首地址

int n:方阵的行数

【输出】无*/

void fillA(int *mtrx, int n)

{

int wThickness[4]={0, 0, 0, 0}; //wall thickness,将矩阵的外圈已填充了数字的看成一堵“墙”,wThickness存储了墙的厚度

int i, j;

int nStep;

int direct;

int row, col;

i=1;

direct=/*DOWN; //初始方向是“下”*/RIGHT;//初始方向是RIGHT

row=-1, col=0; //记住当前的行和列的位置

while(i<=n*n) //n*n cells in total to be filled,从数字1开始,总共将n*n个数字(1, 2, 3, ...)填入矩阵

{

//printf("i=%d\n",i);

//getchar();

switch(direct) //处理不同的方向

{

/*case DOWN: //下

nStep=n-(wThickness[DOWN]+wThickness[UP]); //往下能走多少格?方阵的行数减去上方和下方墙的厚度

printf("direct=%d, nStep=%d\n", direct, nStep);

for(j=1; j<=nStep; j++) //将数字填写到恰当位置

{

row++;

*(mtrx+row*n+col)=i; //实质要达到的目的是: mtrx[row][col]=i

i++;

}

wThickness[LEFT]++; //左侧的墙的厚度增加1

break;

*/

case RIGHT: //右

nStep=n-(wThickness[RIGHT]+wThickness[LEFT]); //往右能走多少格?方阵的列数减去右侧和左侧的厚度

printf("direct=%d, nStep=%d\n", direct, nStep);

for(j=1; j<=nStep; j++) //将数字填写到恰当位置

{

col++;

*(mtrx+row*n+col)=i; //实质要达到的目的是: mtrx[row][col]=i;

i++;

}

/*    wThickness[DOWN]++; //下方的墙的厚度增加1*/

wThickness[UP]++; //上方的墙的厚度增加1

break;

case DOWN: //下

nStep=n-(wThickness[DOWN]+wThickness[UP]); //往下能走多少格?方阵的行数减去上方和下方墙的厚度

printf("direct=%d, nStep=%d\n", direct, nStep);

for(j=1; j<=nStep; j++) //将数字填写到恰当位置

{

/*    row++;*/

row--;

*(mtrx+row*n+col)=i; //实质要达到的目的是: mtrx[row][col]=i

i++;

}

/*    wThickness[LEFT]++; //左侧的墙的厚度增加1*/

wThickness[RIGHT]++; //右侧的墙的厚度增加1

break;

case LEFT: //左

nStep=n-(wThickness[RIGHT]+wThickness[LEFT]); //往左能走多少格?方阵的列数减去右侧和左侧的厚度

printf("direct=%d, nStep=%d\n", direct, nStep);

for(j=1; j<=nStep; j++) //将数字填写到恰当位置

{

col--;

*(mtrx+row*n+col)=i; //实质要达到的目的是: mtrx[row][col]=i;

i++;

}

/*    wThickness[UP]++; //上方的墙的厚度增加1 */

wThickness[DOWN]++; //上方的墙的厚度增加1

break;

case UP: //上

nStep=n-(wThickness[DOWN]+wThickness[UP]);   //往上能走多少格?方阵的行数减去上方和下方墙的厚度

printf("direct=%d, nStep=%d\n", direct, nStep);

for(j=1; j<=nStep; j++) //将数字填写到恰当位置

{

/*    row--;*/

row++;

*(mtrx+row*n+col)=i; //实质要达到的目的是: mtrx[row][col]=i;

i++;

}

/*    wThickness[RIGHT]++; //右侧的墙的厚度增加1*/

wThickness[DOWN]++; //右侧的墙的厚度增加1

break;

/*    case LEFT: //左

nStep=n-(wThickness[RIGHT]+wThickness[LEFT]); //往左能走多少格?方阵的列数减去右侧和左侧的厚度

printf("direct=%d, nStep=%d\n", direct, nStep);

for(j=1; j<=nStep; j++) //将数字填写到恰当位置

{

col--;

*(mtrx+row*n+col)=i; //实质要达到的目的是: mtrx[row][col]=i;

i++;

}

wThickness[UP]++; //上方的墙的厚度增加1

break;

*/

default: //should not run here

printf("error in fillA()\n");

break;

}

direct=(direct+1)%4;

}

}

int main(void)

{

{//fixed size,预先固定大小的方阵

fillA(&gMtrx[0][0],N);

printMtrx(&gMtrx[0][0],N);

}

{//variable size,预先不固定方阵的大小,由用户指定大小

int n;

int *mtrx=NULL;

printf("pls enter a postive integer less than 100:\n");

fflush(stdin);

scanf("%d",&n);

mtrx=(int *)malloc(sizeof(int)*n*n);

fillA(mtrx,n);

printMtrx(mtrx,n);

free(mtrx);

}

return 0;

}

搜索更多相关主题的帖子:

include

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值