-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29_Print_Matrix_Cycle.cpp
More file actions
145 lines (110 loc) · 3.05 KB
/
29_Print_Matrix_Cycle.cpp
File metadata and controls
145 lines (110 loc) · 3.05 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//
// Created by mark on 2019/7/10.
// Copyright © 2019年 mark. All rights reserved.
//
/*
说明:
1. 问题:29.顺时针打印矩阵
2. 思路:根据矩阵的行和列数,依次旋转打印矩阵;每旋转依次,左上角后退一个单位,右下角前进一个单位;
注意单行和单列情况
*/
#include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
using namespace std;
void PrintMatrixInCycle(int** nums, int columns, int rows, int start);
// 判断循环打印条件
void PrintMatrix(int** nums, int columns, int rows)
{
if(nums == nullptr || columns <= 0 || rows <= 0)
return;
int start = 0;
while(columns > start * 2 && rows > start * 2)
{
PrintMatrixInCycle(nums,columns,rows,start);
++start;
}
}
// 循环打印
void PrintMatrixInCycle(int** nums, int columns, int rows, int start)
{
int end_x = columns - 1 - start; // 表示剩下列数
int end_y = rows - 1 - start; // 表示剩下行数
for(int i = start; i <= end_x; ++i) // 从左到右打印一行
{
int num = nums[start][i];
cout << num << " ";
}
if(start < end_y) // 从上到下打印一列
{
for(int i = start + 1; i <= end_y; ++i)
{
int num = nums[i][end_x];
cout << num << " ";
}
}
if(start < end_x && start < end_y) // 从右向左打印一行
{
for(int i = end_x - 1; i >= start; --i)
{
int num = nums[end_y][i];
cout << num << " ";
}
}
if(start < end_x && start < end_y) // 从下到上打印一列
{
for(int i = end_y - 1; i >= start + 1; --i)
{
int num = nums[i][start];
cout << num << " ";
}
}
}
// 辅助函数-----------------------------------------------------------------------------
// 生成矩阵
int** CreatMatrix(int rows,int cols)
{
if(rows < 1 || cols < 1)
cout << "没有数字!";
int sum = 1;
int** nums = new int*[rows];
for(int i = 0; i < rows; ++i)
{
nums[i] = new int[cols];
for(int j = 0; j < cols; ++j)
{
nums[i][j] = sum;
sum++;
}
}
return nums;
}
// 打印输入的矩阵
void Print_Original_Matrix(int** matrix, int rows, int cols)
{
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
//cout << matrix[i][j] << " ";
printf("%d\t", matrix[i][j]); // \t有制表符,使得输出间隔整齐
}
cout << endl;
}
}
int main(){
int cols,rows;
cout << "请输入行数和列数:";
cin >> rows >> cols;
int** nums = CreatMatrix(rows,cols);
cout << "原矩阵是:" << endl;
Print_Original_Matrix(nums, rows, cols);
cout << endl;
cout << "逆序打印是:";
PrintMatrix(nums, cols, rows);
cout << endl;
return 0;
}