问题:为什么矩阵要用一维数组表示呢?
因为我对C语言还很不熟,在我写的过程中,因为要抽成函数,二维数组的传参怎么写编译都不通过(脑子里都是Java
的语法)。
而在C语言中,二维数组在内存中是连续的,表现形式和一维数组一样,通过控制下标可以实现二维数组的效果。
1.左右,上下翻转
void trans_x(int arr[], int m, int n) {
for (int i = 0; i < m; ++i) {
for (int j = n - 1; j >= 0; --j) {
printf("%d ", arr[i * n + j]);
}
printf("\n");
}
}
void trans_y(int arr[], int m, int n) {
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < n; ++j) {
printf("%d ", arr[i * n + j]);
}
printf("\n");
}
}
这个很简单,左右翻转,行的控制条件不变,列逆序输出,上下翻转同理
2.左右旋转
void spin_left(int arr[], int m, int n) {
for (int i = 0; i < n; ++i) {
int j;
for (j = 0; j < m; ++j) {
printf("%d ", arr[j * n + (n - i - 1)]);
}
printf("\n");
}
}
void spin_right(int arr[], int m, int n) {
for (int i = 0; i < n; ++i) {
int j;
for (j = 0; j < m; ++j) {
printf("%d ", arr[(m - j - 1) * n + i]);
}
printf("\n");
}
}
- 旋转后,m 行 n 列的矩阵转成 n 行 m 列,因此行列的 n m 控制条件交换
- 以右转为例,原本第 j 行的数据转成第 m - j - 1 列,第 i 列的数据转成第 i 行, 因此 arr[ i ][ j ] 右转后成为
arr[ m - i - 1 ][ j ] ,加上一维数组控制下标就成为 (m - i - 1) * n + j ,n m 调转后就成了 (m - j - 1) * n + i
3.螺旋
int get_loop(int m, int n) {
if (m < 3 || n < 3) {
return 1;
} else {
return get_loop(m - 2, n - 2) + 1;
}
}
void trans_spiral(const int arr[], int m, int n) {
int loop_times = 0, index = 0;
int loop = get_loop(m, n);
int res[m * n];
while (loop_times < loop) {
if (loop_times == n - loop_times - 1) {
for (int i = loop_times; i < m - loop_times; ++i) {
res[index++] = arr[i * n + loop_times];
}
break;
}
for (int i = loop_times; i < n - loop_times - 1; ++i) {
res[index++] = arr[loop_times * n + i];
}
for (int i = loop_times; i < m - loop_times - 1; ++i) {
res[index++] = arr[i * n + (n - loop_times - 1)];
}
for (int i = n - loop_times - 1; i > loop_times; --i) {
res[index++] = arr[(m - loop_times - 1) * n + i];
}
for (int i = m - loop_times - 1; i > loop_times; --i) {
res[index++] = arr[i * n + loop_times];
}
loop_times++;
}
for (int i = 0; i < m * n; ++i) {
printf("%d ", res[i]);
}
}
3.1基本思路
- 先判断 m 行 n 列需要循环几圈,如果小于3行,或者小于3列,都只需要1圈,否则每执行完1圈,剥去2行2列,递归查询
- 每一次循环都是 → ↓ ← ↑,在上一次的基础上长度缩小一圈
4.完整代码
#include <stdio.h>
void trans_x(int arr[], int m, int n) {
for (int i = 0; i < m; ++i) {
for (int j = n - 1; j >= 0; --j) {
printf("%d ", arr[i * n + j]);
}
printf("\n");
}
}
void trans_y(int arr[], int m, int n) {
for (int i = m - 1; i >= 0; --i) {
for (int j = 0; j < n; ++j) {
printf("%d ", arr[i * n + j]);
}
printf("\n");
}
}
void spin_left(int arr[], int m, int n) {
for (int i = 0; i < n; ++i) {
int j;
for (j = 0; j < m; ++j) {
printf("%d ", arr[j * n + (n - i - 1)]);
}
printf("\n");
}
}
void spin_right(int arr[], int m, int n) {
for (int i = 0; i < n; ++i) {
int j;
for (j = 0; j < m; ++j) {
printf("%d ", arr[(m - j - 1) * n + i]);
}
printf("\n");
}
}
int get_loop(int m, int n) {
if (m < 3 || n < 3) {
return 1;
} else {
return get_loop(m - 2, n - 2) + 1;
}
}
void trans_spiral(const int arr[], int m, int n) {
int loop_times = 0, index = 0;
int loop = get_loop(m, n);
int res[m * n];
while (loop_times < loop) {
if (loop_times == n - loop_times - 1) {
for (int i = loop_times; i < m - loop_times; ++i) {
res[index++] = arr[i * n + loop_times];
}
break;
}
for (int i = loop_times; i < n - loop_times - 1; ++i) {
res[index++] = arr[loop_times * n + i];
}
for (int i = loop_times; i < m - loop_times - 1; ++i) {
res[index++] = arr[i * n + (n - loop_times - 1)];
}
for (int i = n - loop_times - 1; i > loop_times; --i) {
res[index++] = arr[(m - loop_times - 1) * n + i];
}
for (int i = m - loop_times - 1; i > loop_times; --i) {
res[index++] = arr[i * n + loop_times];
}
loop_times++;
}
for (int i = 0; i < m * n; ++i) {
printf("%d ", res[i]);
}
}
int main() {
int arr[] = {
38, 42, 21, 31, 10, 15, 94,
30, 38, 41, 41, 68, 11, 59,
92, 46, 55, 90, 43, 6, 16,
63, 11, 6, 75, 57, 71, 64,
4, 89, 94, 45, 45, 55, 59,
15, 87, 82, 65, 15, 8, 11,
89, 55, 25, 86, 18, 18, 86,
9, 7, 14, 95, 81, 99, 34,
1, 50, 5, 40, 58, 23, 21,
};
trans_x(arr, 9, 7);
printf("\n-------------------------------\n");
trans_y(arr, 9, 7);
printf("\n-------------------------------\n");
spin_left(arr, 9, 7);
printf("\n-------------------------------\n");
spin_right(arr, 9, 7);
printf("\n-------------------------------\n");
trans_spiral(arr, 9, 7);
return 0;
}
运行结果
94 15 10 31 21 42 38
59 11 68 41 41 38 30
16 6 43 90 55 46 92
64 71 57 75 6 11 63
59 55 45 45 94 89 4
11 8 15 65 82 87 15
86 18 18 86 25 55 89
34 99 81 95 14 7 9
21 23 58 40 5 50 1
-------------------------------
1 50 5 40 58 23 21
9 7 14 95 81 99 34
89 55 25 86 18 18 86
15 87 82 65 15 8 11
4 89 94 45 45 55 59
63 11 6 75 57 71 64
92 46 55 90 43 6 16
30 38 41 41 68 11 59
38 42 21 31 10 15 94
-------------------------------
94 59 16 64 59 11 86 34 21
15 11 6 71 55 8 18 99 23
10 68 43 57 45 15 18 81 58
31 41 90 75 45 65 86 95 40
21 41 55 6 94 82 25 14 5
42 38 46 11 89 87 55 7 50
38 30 92 63 4 15 89 9 1
-------------------------------
1 9 89 15 4 63 92 30 38
50 7 55 87 89 11 46 38 42
5 14 25 82 94 6 55 41 21
40 95 86 65 45 75 90 41 31
58 81 18 15 45 57 43 68 10
23 99 18 8 55 71 6 11 15
21 34 86 11 59 64 16 59 94
-------------------------------
38 42 21 31 10 15 94 59 16 64 59 11 86 34 21 23 58 40 5 50 1 9 89 15 4
63 92 30 38 41 41 68 11 6 71 55 8 18 99 81 95 14 7 55 87 89 11 46 55
90 43 57 45 15 18 86 25 82 94 6 75 45 65
|