顺时针螺旋矩阵
题目描述小蓝鲸在计算机中用二维数组的形式存储了一个个矩阵。现给定一个包含m*n个元素的矩阵m行n列请帮助小蓝鲸按照顺时针螺旋顺序返回矩阵中的所有元素。输入格式第一行包含一个整数m表示矩阵的行数。第二行包含一个整数n表示矩阵的列数。接下来m行每行n个整数表示矩阵每行的各个元素。输出格式顺时针螺旋顺序返回的元素每个元素之间用空格隔开。示例Input:3 3 1 2 3 4 5 6 7 8 9Output:1 2 3 6 9 8 7 4 5数据说明1 m, n 201 数据大小 100时间限制1s空间限制128MBC实现#include iostream using namespace std; int main() { int m, n; cin m; cin n; int arr[m][n]; for (int i 0; i m; i) { for (int j 0; j n; j) { cin arr[i][j]; } } int left 0; int right n - 1; int top 0; int bottom m - 1; while (left right top bottom) { for (int j left; j right; j) { cout arr[top][j] ; } top; for (int i top; i bottom; i) { cout arr[i][right] ; } right--; if (top bottom) { for (int j right; j left; j--) { cout arr[bottom][j] ; } bottom--; } if (left right) { for (int i bottom; i top; i--) { cout arr[i][left] ; } left; } } return 0; }