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

A24152. 图像旋转

填空题 中等

题目描述

图像旋转

题目描述

输入一个n行m列的黑白图像,将它顺时针旋转90度后输出。

输入

第一行包含两个整数n和m,表示图像包含像素点的行数和列数。1≤n≤100,1≤m≤100。

接下来n行,每行m个整数,表示图像的每个像素点灰度。相邻两个整数之间用单个空格隔开,每个元素均在0∼255之间。

输出

m行,每行n个整数,为顺时针旋转90度后的图像。相邻两个整数之间用单个空格隔开。

输入样例

3 3
1 2 3
4 5 6
7 8 9

输出样例

7 4 1
8 5 2

参考答案

#include<bits/stdc++.h> using namespace std; #define N 105 int main() { int n, m, a[N][N], b[N][N]; cin >> n >> m; for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) cin >> a[i][j]; for(int i = 1; i <= n; ++i)//遍历原矩阵a,n行m列 for(int j = 1; j <= m; ++j) b[j][n - i + 1] = a[i][j]; for(int i = 1; i <= m; ++i)//遍历矩阵b,m行n列 { for(int j = 1; j <= n; ++j) cout << b[i][j] << ' '; cout << endl; } return 0; }

答案解析


#include<bits/stdc++.h>
using namespace std;
#define N 105
int main()
{
    int n, m, a[N][N], b[N][N];
    cin >> n >> m;
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= m; ++j)
            cin >> a[i][j];
    for(int j = 1; j <= m; ++j)//遍历矩阵b,m行n列
    {
        for(int i = n; i >= 1; --i)
            cout << a[i][j] << ' ';
        cout << endl;
    }
    return 0;
}


上一题 下一题