一、螺旋打印数组
给定一个整型矩阵matrix, 按照转圈的方式打印它
1.1 解答


1.2 测试结果及代码

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <climits>
#include <iomanip>
#include <stack>
using namespace std;
typedef vector<int> vInt;
void printMatrix(const vector<vInt> &M) {
for (int i = 0; i < M.size(); ++i) {
for (int j = 0; j < M[0].size(); ++j) {
cout << setw(2) << M[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void printEdge(const vector<vInt> &M, int a, int b, int c, int d) {
if (a == c) {
int i = b;
while (i <= d) {
cout << M[a][i++] << "-";
}
cout << endl;
} else if (b == d) {
int i = a;
while (i <= c) {
cout << M[i++][b] << "-";
}
cout << endl;
} else {
int row = a;
int col = b;
while (col != d) {
cout << M[row][col++] << "-";
}
while (row != c) {
cout << M[row++][col] << "-";
}
while (col != b) {
cout << M[row][col--] << "-";
}
while (row != a) {
cout << M[row--][col] << "-";
}
}
}
void spiralOrderPrint(const vector<vInt> &M) {
int lu_R = 0;
int lu_C = 0;
int rd_R = M.size() - 1;
int rd_C = M[0].size() - 1;
while (lu_R <= rd_R && lu_C <= rd_C) {
printEdge(M, lu_R++, lu_C++, rd_R--, rd_C--);
}
}
int main (int argc, char* argv[]) {
srand(time(NULL));
const int test_time = 15; // 测试次数
for (int cnt = 0; cnt < test_time; ++cnt) {
int rows = rand() % 10 + 1;
int cols = rand() % 10 + 1;
vector<vInt> Matrix(rows);
for (int i = 0; i < rows; ++i)
Matrix[i].resize(cols);
// vector<vInt> Matrix(rows, vInt(cols));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
Matrix[i][j] = rand() % 99 + 1;
}
}
cout << "\n第 " << cnt << " 次生成:\n";
printMatrix(Matrix);
cout << "第 " << cnt << " 次路线:\n";
spiralOrderPrint(Matrix);
cout << endl;
}
return 0;
}
二、旋转数组
2.1 解法
思路同上,外圈转好了转内圈即可

2.2 测试结果及代码

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <climits>
#include <iomanip>
#include <stack>
using namespace std;
typedef vector<int> vInt;
void printMatrix(const vector<vInt> &M) {
for (int i = 0; i < M.size(); ++i) {
for (int j = 0; j < M[0].size(); ++j) {
cout << setw(2) << M[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void Swap(int &x, int &y) {
int t = x;
x = y;
y = t;
}
void rotate(vector<vInt> &M, int lu_r, int lu_c, int rd_r, int rd_c) {
int a = lu_r;
int b = lu_c;
int c = rd_r;
int d = rd_c;
int times = rd_c - lu_c;
while (times-- > 0) {
// 顺时针
Swap(M[lu_r][b], M[a++][rd_c]);
Swap(M[lu_r][b], M[rd_r][d--]);
Swap(M[lu_r][b], M[c--][lu_c]);
// 逆时针,注意换的顺序即可
// int tmp = M[lu_r][b];
// M[lu_r][b] = M[a][rd_c];
// M[a][rd_c] = M[rd_r][d];
// M[rd_r][d] = M[c][lu_c];
// M[c][lu_c] = tmp;
// a++;
// b++;
// c--;
// d--;
}
}
void rotateMatrix(vector<vInt> &M) {
int lu_R = 0;
int lu_C = 0;
int rd_R = M.size() - 1;
int rd_C = M[0].size() - 1;
while (lu_R < rd_R) {
rotate(M, lu_R++, lu_C++, rd_R--, rd_C--);
}
}
int main (int argc, char* argv[]) {
srand(time(NULL));
const int test_time = 15; // 测试次数
for (int cnt = 0; cnt < test_time; ++cnt) {
int N = rand() % 10 + 1;
vector<vInt> Matrix(N);
for (int i = 0; i < N; ++i)
Matrix[i].resize(N);
// vector<vInt> Matrix(rows, vInt(cols));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
Matrix[i][j] = rand() % 99 + 1;
}
}
cout << "\n第 " << cnt << " 次生成:\n";
printMatrix(Matrix);
cout << "第 " << cnt << " 次旋转:\n";
rotateMatrix(Matrix);
printMatrix(Matrix);
cout << endl;
}
return 0;
}
三、之形打印数组

3.1 解法

3.2 测试结果及代码

#include <iostream>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <climits>
#include <iomanip>
#include <stack>
using namespace std;
typedef vector<int> vInt;
void printMatrix(const vector<vInt> &M) {
for (int i = 0; i < M.size(); ++i) {
for (int j = 0; j < M[0].size(); ++j) {
cout << setw(2) << M[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
void printLevel(vector<vInt> &M, int aR, int aC, int bR, int bC, bool flag) {
if (flag) {
while (bR != aR - 1) {
cout << M[bR--][bC++] << " ";
}
} else {
while (aR != bR + 1) {
cout << M[aR++][aC--] << " ";
}
}
}
void printMatrixZigZag(vector<vInt> & M) {
int aR = 0;
int aC = 0;
int bR = 0;
int bC = 0;
int R_end = M.size() - 1;
int C_end = M[0].size() - 1;
bool down2up = true;
while (aR != R_end + 1) { // 也可写成bc != C_end + 1
printLevel(M, aR, aC, bR, bC, down2up);
aR = ((aC == C_end) ? (aR + 1) : aR);
aC = ((aC == C_end) ? aC : (aC + 1));
bC = ((bR == R_end) ? (bC + 1) : bC);
bR = ((bR == R_end) ? bR : (bR + 1));
// bC = ((bR == R_end) ? (bC + 1) : bC);
down2up = !down2up;
}
}
int main(int argc, char *argv[])
{
srand(time(NULL));
const int test_time = 15; // 测试次数
for (int cnt = 0; cnt < test_time; ++cnt) {
int N = rand() % 10 + 1;
vector<vInt> M(N);
for (int i = 0; i < N; ++i)
M[i].resize(N);
// vector<vInt> M(rows, vInt(cols));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
M[i][j] = rand() % 99 + 1;
}
}
cout << "\n第 " << cnt << " 次生成:\n";
printMatrix(M);
cout << "第 " << cnt << " 次zigzag:\n";
printMatrixZigZag(M);
M.clear();
cout << endl;
}
return 0;
}
本文介绍三种数组打印方法:螺旋打印、旋转数组和之形打印,通过实例代码详细解析每种打印方式的实现思路和步骤。

1304

被折叠的 条评论
为什么被折叠?



