首页 › 题库 › C/C++编程考级 › C/C++编程三级 › 简答题
简答题 偏难
#12904

画作 题目描述 墙上,挂着一幅由 n×n 个彩色方格组成的画作。画作的内容可以用一个 n×n 的字符矩阵来表示。 现在想要将这幅画顺时针旋转 90 度后重新悬挂…

画作

题目描述

墙上,挂着一幅由 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

未登录也能判分,登录后才会记录
答案与解析
正确答案:<pre class="brush:cpp;toolbar:false">//参考代码1 #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;string&gt; 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 &gt;&gt; n;     // 读取原图     for (int i = 0; i &lt; n; i++)     {         string s;         cin &gt;&gt; s;         for (int j = 0; j &lt; n; j++)         {             mat[i][j] = s[j];         }     }     // 顺时针90度旋转赋值     for (int i = 0; i &lt; n; i++)     {         for (int j = 0; j &lt; n; j++)         {             res[j][n - 1 - i] = mat[i][j];         }     }     // 输出结果     for (int i = 0; i &lt; n; i++)     {         for (int j = 0; j &lt; n; j++)         {             cout &lt;&lt; res[i][j];         }         cout &lt;&lt; '\n';     }     return 0; }</pre><pre class="brush:cpp;toolbar:false">//参考代码2 #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;algorithm&gt; using namespace std; const int MAXN = 1005; char mat[MAXN][MAXN]; int main() {     ios::sync_with_stdio(false);     cin.tie(nullptr);     int n;     cin &gt;&gt; n;     for (int i = 0; i &lt; n; i++)     {         string s;         cin &gt;&gt; s;         for (int j = 0; j &lt; n; j++)             mat[i][j] = s[j];     }     // 1. 上下翻转     for (int i = 0; i &lt; n / 2; i++)     {         for (int j = 0; j &lt; n; j++)             swap(mat[i][j], mat[n - 1 - i][j]);     }     // 2. 主对角线交换     for (int i = 0; i &lt; n; i++)     {         for (int j = i + 1; j &lt; n; j++)             swap(mat[i][j], mat[j][i]);     }     // 输出     for (int i = 0; i &lt; n; i++)     {         for (int j = 0; j &lt; n; j++)             cout &lt;&lt; mat[i][j];         cout &lt;&lt; '\n';     }     return 0; }</pre>
这道题暂未补解析。