荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: tecky (...), 信区: Program
标  题: Re: 一个TC问题
发信站: 荔园晨风BBS站 (Mon Oct 21 17:25:38 2002), 站内信件

  不用那么繁,这样就行了。

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAXN 20

void main()
{
        int num[MAXN][MAXN];                /* 用来存放数字阵列(只用到左上角那
块)
*/
        int step[4] = {1, MAXN, -1, -MAXN}; /* pos在阵列中移动的步长枚举值(代表
四个方
向) */
        int i, j;                           /* 通用计数器 */
        int n, n_sqr;                       /* 阶数及其平方 */
        int pos = -1;                       /* 当前数字在阵列中应有的位置 */
        int direction = 0;                  /* 当前行进方向(初值:向右) */
        int count;                          /* 当前方向的维持次数 */
        int flag = 1;                       /* count是否需要减一 */

        /* 输入n(在1-20之内) */
        printf("Please input n:(1-20) ");
        scanf("%d", &n);
        if (n<1 || n>20)
        {
                printf("Input error!\n");
                getch();
                exit(0);
        }

        /* 计算n阶阵列 */
        n_sqr = n*n;
        count = n;
        for (i=1, j=count; i<=n_sqr; i++, j--)
        {
                if (!j)                          /* 该转方向了吗? */
                {
                        direction = (++direction)%4; /* 方向以上、下、左、右循环
 */
                        if (flag) count--;           /* 对该方向行进步数进行调整
 */
                        j = count;                   /* 初始化计数器 */
                        flag = !flag;                /* 二值循环 */
                }
                pos += step[direction];          /* 数字i在阵列中应有的位置 */
                *((int *)num+pos) = i;
        }

        /* 显示n阶阵列 */
        for (i=0; i<n; i++)
        {
                for (j=0; j<n; j++)
                        printf("%3d ", num[i][j]);
                printf("\n");
        }
        printf("\n");

        getch();
}

  如果还是不明白,看看下面这个图就很清楚了。^_^
   1    2    3   4
------------------
  12 | 13   14 | 5
     |---------|
  11 | 16 | 15 | 6
---------------|
  10    9    8 | 7



【 在 bigone (survival.....) 的大作中提到: 】
: //我也弄了一个,大家参考参考:)
: #include <stdio.h>
: #include <conio.h>
: void main()
: {
:         int             allay[20][20];
:         int             x,      y;
:         int             n;
:         int             TOP,    BOTTOM, LEFT,    RIGHT;
:         int             max_num, cur_num;
:         int             x_to,   y_to;
:         printf("input n:");
:         scanf("%d",&n);
:         max_num = n * n;
:         cur_num = 1;
:         TOP = 1;
:         BOTTOM = n;
:         LEFT = 1;
:         RIGHT = n;
:         x = 1;
:         y = 1;
:         while (1)       {
:                 for (x_to = RIGHT,y_to = TOP; x <= x_to; x++)   {
:                         allay[x][y] = cur_num;
:                         cur_num++;
:                 }
:                 TOP++;
:                 x--;
:                 y++;
:                 for (x_to = RIGHT,y_to = BOTTOM; y <= y_to; y++)        {
:                         allay[x][y] = cur_num;
:                         cur_num++;
:                 }
:                 RIGHT--;
:                 y--;
:                 x--;
:                 for (x_to = LEFT,y_to = BOTTOM; x >= x_to; x--)         {
:                         allay[x][y] = cur_num;
:                         cur_num++;
:                 }
:                 if (cur_num == max_num+1)
:                         break;
:                 BOTTOM--;
:                 x++;
:                 y--;
:                 for (x_to = LEFT,y_to = TOP; y >= y_to; y--)    {
:                         allay[x][y] = cur_num;
:                         cur_num++;
:                 }
:                 LEFT++;
:                 y++;
:                 x++;
:         }
:         clrscr();
:         for (y = 1;y <= n;y++){
:                 printf("\n\n");
:                 for(x = 1;x <= n;x++)
:                         printf("%4d",allay[x][y]);
:         }
:         getch();
: 【 在 lr (三戒) 的大作中提到: 】
: : 编写一C程序,对输出的正整数n(n<80),能构造并输出如下所示方阵(n=4时)
: :  1  2  3  4
: : 12 13 14  5
: : 11 16 15  6
: : 10  9  8  7


--

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.0.222]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店