C++之旋转矩阵和打印一个有规律的矩阵

旋转数组

描述:

某个图像通过一个整数组成的m*n矩阵表示,其中每个整数表示一个像素值。写出一种方法,根据flag变量的值将图像向右或者向左旋转90°。如果flag值为0,则向左旋转,如果flag为1,则向右旋转。

函数rotatePictureMethod的输入分别由矩阵matrix、矩阵的维度m和n以及flag的值组成。

函数应返回一个指向二维矩阵指针,该矩阵是按照flag值旋转后的结果矩阵而动态分配的。

示例:

如果标志flag=1且m=3,n=3,

输入矩阵

1   2   3

4   5   6

7   8   9

输出矩阵

7   4   1

8   5   2

9   6   3

思路:

逆时针旋转:

1 交换主对角线对称的元素;

2 交换第i行和第n-1-i行;

顺时针旋转:

1 交换副对角线对称的元素;

2 交换第i行和第n-1-i行;

原图:           第一步操作后:   第二步操作后:
1 2 3 4         1 5 9 13        4 8 12 16
5 6 7 8         2 6 10 14       3 7 11 15
9 10 11 12      3 7 11 15       2 6 10 14
13 14 15 16     4 8 12 16       1 5 9 13

 

C++代码:

#include <iostream>
using namespace std;

void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}

// 逆时针旋转
void transpose(int a[][4], int n)
{
    for (int i = 0; i < n; i++)
        for (int j = i+1; j < n; j++)
            swap(a[i][j], a[j][i]);
    for (int i = 0; i < n / 2; i++)
        for (int j = 0; j < n; j++)
            swap(a[i][j], a[n - 1 - i][j]);
}

// 顺时针旋转
void clockwise(int a[][4], int n)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n - i; j++)
            swap(a[i][j], a[n - 1 - j][n - 1 - i]);
    for (int i = 0; i < n / 2; i++)
        for (int j = 0; j < n; j++)
            swap(a[i][j], a[n - 1 - i][j]);
}

int main(int argc, char** argv)
{
    int a[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    cout << "原矩阵" << endl;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j)
            cout << a[i][j] << " ";
        cout << endl;
    }
    cout << endl;
    transpose(a, 4);
    cout << "逆时针转90度" << endl;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j)
            cout << a[i][j] << " ";
        cout << endl;
    }
    cout << endl;
    
    int a2[4][4] = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12},
        {13, 14, 15, 16}
    };
    cout << "原矩阵" << endl;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j)
            cout << a2[i][j] << " ";
        cout << endl;
    }
    clockwise(a2, 4);
    cout << endl;
    cout << "顺时针转90度" << endl;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j)
            cout << a2[i][j] << " ";
        cout << endl;
    }
    
    return 0;
}

 

 

打印一个有规律的矩阵

描述:

给定一个整数n,以下列方式打印n行。

如果n=4,生成的排列将为:

1*2*3*4

9*10*11*12

13*14*15*16

5*6*7*8

函数squarePatternPrint的输入应包括一个整数n(假设0<=n<=100)。不要从函数返回任何内容。使用cout打印所需的阵列。

各输出行只能由“数字”和“*”组成,不应有空格。

有用的命令:cout可将内容打印到屏幕上。

思路:

如果n为偶数,就打印n/2行,如果n为奇数,就打印n/2+1行;

以n=4举例,先打印1*2*3*4,把5压进栈,5+n = 9,打印9*10*11*12,把13压进栈,此时从栈顶读取13,打印13*14*15*16,弹出栈顶元素13,继续从栈顶读取5,打印5*6*7*8,判断此时栈内为空,end.

这种思路有些繁琐,我觉得肯定有更好的思路,只是我没想起来,如果你有更好的思路,欢迎评论!

C++代码:

#include <iostream>
#include <stack>
using namespace std;

void sprint(int n)
{
    stack<int> nums;
    int i = 1, j = 1, flag = 0;
    if (n % 2 == 0)
        flag = n / 2;
    else
        flag = n / 2 + 1;
    for (; i <= flag; i++)
    {
        for (int k = 0; k < n; k++)
        {
            if (j % n == 0) cout << j;
            else cout << j << "*";
            j++;
        }
        nums.push(j);
        j = j + n;
        cout << endl;
    }
    if (n % 2 != 0)
        nums.pop();
    while (!nums.empty())
    {
        int top = nums.top();
        nums.pop();
        for (int k = 0; k < n; k++)
        {
            if (top % n == 0) cout << top;
            else cout << top << "*";
            top++;
        }
        cout << endl;
    }
}

int main()
{
    sprint(3);
}

 

参考:

http://www.voidcn.com/article/p-wusaguhx-bbb.html

https://blog.csdn.net/qq_26525215/article/details/52076488

 

原文地址:https://www.cnblogs.com/zkfopen/p/11094268.html