2.1.16 —线性表—Rotate Image

来源:互联网 发布:英雄杀这次探宝数据 编辑:程序博客网 时间:2024/06/05 03:24
描述
You are given an n × n2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).

Follow up: Could you do this in-place?

#include<iostream>#include<vector>using namespace std;const int n = 4;void RotataImage(int a[][n], int m){for (int i = 0; i < m; i++){for (int j = 0; j < n - i - 1; j++){int temp = a[i][j];a[i][j] = a[n - j - 1][n - i - 1];a[n - j - 1][n - i - 1] = temp;}}for (int i = 0; i < m / 2; i++){for (int j = 0; j < n; j++){int temp = a[i][j];a[i][j] = a[n - i - 1][j];a[n - i - 1][j] = temp;}}}int main(){int a[n][n] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };RotataImage(a, n);for (int i = 0; i < n; i++){for (int j = 0; j < n; j++)cout << a[i][j] << " ";cout << endl;}}


原创粉丝点击