测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A17438. 画作

填空题 中等

题目描述

画作

题目描述

墙上,挂着一幅由 n×n 个彩色方格组成的画作。画作的内容可以用一个 n×n 的字符矩阵来表示。

现在想要将这幅画顺时针旋转 90 度后重新悬挂,请你他计算出旋转后的画作是什么样的。

输入格式

第一行,一个整数表示 n

接下来 n 行,每行 n 个字符。

输出格式

输出 n 行,每行 n 个字符,表示顺时针旋转 90 度后的矩阵。

输入样例#1

4
ooxx
xoox
xxxx
xxxx

输出样例#1

xxxo
xxoo
xxox
xxxx

输入样例#2

2
12
34

输出样例#2

31
42

说明提示

1≤n≤1000

参考答案

//参考代码1 #include <iostream> #include <vector> #include <string> using namespace std; const int MAXN = 1005; char mat[MAXN][MAXN]; char res[MAXN][MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); // 加速输入输出,n=1000必加 int n; cin >> n; // 读取原图 for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) { mat[i][j] = s[j]; } } // 顺时针90度旋转赋值 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { res[j][n - 1 - i] = mat[i][j]; } } // 输出结果 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << res[i][j]; } cout << '\n'; } return 0; } //参考代码2 #include <iostream> #include <string> #include <algorithm> using namespace std; const int MAXN = 1005; char mat[MAXN][MAXN]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; for (int j = 0; j < n; j++) mat[i][j] = s[j]; } // 1. 上下翻转 for (int i = 0; i < n / 2; i++) { for (int j = 0; j < n; j++) swap(mat[i][j], mat[n - 1 - i][j]); } // 2. 主对角线交换 for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) swap(mat[i][j], mat[j][i]); } // 输出 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cout << mat[i][j]; cout << '\n'; } return 0; }
上一题 下一题