赶集网校招就采用了螺旋输出矩阵作为程序题,要求将矩阵螺旋输出如:

图中6*6矩阵线条所示为输出顺序,如果输出正确的话应该输出1~36有序数字。
我想的是这么做的:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
#include <stdio.h>
//#define LEN 1
//#define LEN 2
//#define LEN 3
#define LEN 4
void printClock(int a[][LEN]){//输出函数
int t;
int i = 0, m = 0;
int j = LEN, n = LEN;
while (i <= j || m <= n)
{
for (t = i; t < j; t++)//输出第m行
{
printf("%d ", a[m][t]);
}
m++;
for (t = m; t < n; t++)//输出第j列
{
printf("%d ", a[t][j - 1]);
}
j--;
for (t = j - 1; t >= i; t--)//输出第n行
{
printf("%d ", a[n - 1][t]);
}
n--;
for (t = n - 1; t >= m; t--)//输出第i列
{
printf("%d ", a[t][i]);
}
i++;
}
printf("\n");
}
void main(){
int a[][1] = {1};
int b[][2] = {1,2,
4,3
};
int c[][3] = {1,2,3,
8,9,4,
7,6,5
};
int d[][4] = {1,2, 3, 4,
12,13,14,5,
11,16,15,6,
10, 9, 8,7
};
int e[][6] = { 1, 2, 3, 4, 5, 6,
20,21,22,23,24, 7,
19,32,33,34,25, 8,
18,31,36,35,26, 9,
17,30,29,28,27,10,
16,15,14,13,12,11
};
printClock(d);
}
|
分别做向右输出,向下输出,向左输出,向上输出,然后就进入一种循环,直到输出结束









发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。