CCF NOI1049. 旋转图像 (C++)

版权声明:代码属于原创,转载请联系作者并注明出处。 https://blog.csdn.net/weixin_43379056/article/details/84952898

1049. 旋转图像

题目描述

输入一个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
9 6 3

数据范围限制

1 <= n <= 100,1 <= m <= 100。

C++代码

#include <iostream>
#include <cassert>

using namespace std;

int main()
{
    const int N = 100;
    const int M = 100;
    int PixelMatrix[N][M];
    int NewPixelMatrix[M][N];

    int n, m;

    cin >> n >> m;

    assert(n>=1 && n<=N);
    assert(m>=1 && m<=M);

    // input black white picture's pixel matrix data 
    for(int row=1; row<=n; row++)
    {
        for(int col=1; col<=m; col++)
        {
            cin >> PixelMatrix[row-1][col-1];
            assert(PixelMatrix[row-1][col-1]>=0 && PixelMatrix[row-1][col-1]<=255);
        }
    }

    // rotate pixel matrix 90 degree clockwise 
    for(int col=1; col<=m; col++)
    {
        for(int row=n; row>=1; row--)
        {
            NewPixelMatrix[col-1][n-row] = PixelMatrix[row-1][col-1];
        }
    }

    // output new pixel matrix data 
    for(int row=1; row<=m; row++)
    {
        for(int col=1; col<=n; col++)
        {
            cout << NewPixelMatrix[row-1][col-1] << " ";
        }
        cout << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43379056/article/details/84952898