c++ - 顺时针旋转二维数组

标签 c++ arrays algorithm for-loop dynamic

我需要创建 ad 二维动态数组,然后创建一个函数来接收二维动态数组,然后将其顺时针旋转 90 度并将其返回给主函数。 但是我不确定为什么我没有得到任何输出?是因为错误的交换技术吗? 我认为索引交换如下:

I J-----------I J
0 0           0 1
0 1           1 1
0 2           2 1

随之而来的是:

for (int i = 0; i <row;i++)

    for(int j = 0;j<col; j++)
    {
        Roti[i+j][row-i]=arr[i][j];

    }

代码:

#include <iostream>
using namespace std;
void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int row, int col)
{
    int **Roti = new int *[row];

    for (int i = 0; i <row;i++)
    {
        Roti[i] = new int [col];
    }


    for (int i = 0; i <row;i++)

        for(int j = 0;j<col; j++)
        {
            Roti[i+j][row-i]=arr[i][j];

        }
    return  Roti;
}

int main()
{
    int *A[3];
    for (int i = 0; i < 3; i++)
    {
        A[i] = new int[3];
        for (int j = 0; j < 3; j++)
        {
            A[i][j] = rand() % 20;
        }
    }
    cout << "The array is :\n";
    print2DArray(A, 3, 3);
    int **ptr;
    ptr=Rotate(A,3,3);
cout<<"-----"<<endl;
    print2DArray(ptr, 3, 3);




    for (int i = 0; i < 3; i++)
    {
        delete[] A[i];
    }
    return 0;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }

}

最佳答案

希望“旋转”不是指常规转置,因为 stackoverflow 上已经有很多答案。 What is the fastest way to transpose a matrix in C++?

对于矩阵的旋转,我修改了您的代码以使其工作,如下所示。逻辑很简单,原始矩阵的第一行成为旋转矩阵的最后一列,使用此逻辑,您可以得出以下代码。希望能帮助到你。

#include <iostream>
using namespace std;

void print2DArray(int **arr, int rows, int cols);
int **Rotate(int **arr, int num_rows, int num_cols);

int main()
{
    int num_rows = 3;
    int num_cols = 3;
    int** A_matrix = new int*[num_rows];
    for (int i = 0; i < num_rows; i++)
        A_matrix[i] = new int[num_cols];

    for (int row = 0; row < num_rows; row++)
    {
        for (int col = 0; col < num_cols; col++)
        {
            A_matrix[row][col] = rand() % 20;
        }
    }

    cout << "The array is :\n";
    print2DArray(A_matrix, 3, 3);

    int** roated_matrix;
    roated_matrix = Rotate(A_matrix, num_rows, num_cols);
    cout << "Rotated array:" << endl;
    print2DArray(roated_matrix, 3, 3);


    return 0;
}

int **Rotate(int **arr, int num_rows, int num_cols)
{
    /* Rotated matrix will have dimensions reverse as well. 
       That's why we have rows and cols reversed in the following lines */
    int** rotated_matrix = new int*[num_cols];
    for (int i = 0; i < num_cols; i++)
        rotated_matrix[i] = new int[num_rows];


    for (int row = 0; row < num_rows; row++)
    {
        int col_rotated_matrix = num_rows - 1 - row; // 1st row shall be copied to the last column and so on
        for (int col = 0; col < num_cols; col++)
        {
            int row_rotated_matrix = col; // rows become columns in the rotated matrix
            rotated_matrix[row_rotated_matrix][col_rotated_matrix] = arr[row][col];
        }
    }
    return rotated_matrix;
}
void print2DArray(int **arr, int rows, int cols)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < cols; j++)
        {
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

它产生以下输出:

The array is :
1 7 14
0 9 4
18 18 2
Rotated array:
18 0 1
18 9 7
2 4 14

关于c++ - 顺时针旋转二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54780041/

相关文章:

c - 如何找到由以下关系定义的级数和 T(n+2) = (Tn+1)^2 + T(n) where T(0)=0 and T(1)=1 in C language

java - 用于 C++ 应用程序的 Java GUI - 这是一个好的设计吗?

c# - 访问对象数组的数组的元素

arrays - 具有数组输入的 Matlab 匿名函数

php - 将 PHP 数组从 mysql 格式转换为 JSON 格式

algorithm - 使用伴随矩阵求根

c++ - C++ DFS 中的迷宫回溯算法?

c++ - MPI_File_read 错误

c++ - 什么 shared_ptr 策略用于异步方案?

c++ - 对 "non-type"模板参数执行规则