n*n(n<20,为正奇数)阶方阵(题型二)

输出如下n*n(n<20,为正奇数)阶方阵(题型二)

 1   2   6   7  15
 3   5   8  14  16
 4   9  13  17  22
10  12  18  21  23
11  19  20  24  25
   (n=5)

分析
该方阵是从左上角出发,然后向斜右下角的逐条对称轴的法线上下折返,数字递增。

#include "stdio.h"
#include "math.h"
#include "stdlib.h"
int main()
{   
    int a[20][20], n, i, j, m=0, x, y, temp, count=1;
    scanf("%d", &n);
    for(i=-(n-1);i<=n-1;i++)
    {
        for(j=0;j<n-abs(i);j++)
        {
            if(m<n) x=m-j;
            else x=n-1-j;
            y=m-x;
            if(m%2){temp=x;x=y;y=temp;}
            a[x][y]=count++;
        }
        m++;
    }
    for(i=0; i<n; i++)
    {   
        for(j=0; j<n; j++)
        printf("%4d", a[i][j]);
        printf("\n");
    }
}

你可能感兴趣的:(矩阵)