c - C 中按列的多维操作

标签 c arrays algorithm pointers multidimensional-array

对于这个功能,我在堆栈溢出的任何地方都找不到很好的定义。

与将整列向上移动或整行移动相关的代码或定理是什么?比如

1 2 3
4 5 6
7 8 9

如果我将最后一列向上推,它将是

1 2 6
4 5 9
7 8 3

如果我将最后一行向右推,它会是

1 2 6
4 5 9
3 7 8

我想知道解决此类问题的所有不同类型的方法。

我知道的一种方法是找到一个数组,因为多维数组只是数组的数组,我可以将该数组收集到一个新数组中吗?指针?解释。这是一个很难掌握的概念。

最佳答案

要旋转一列或一行,您可以从将一个元素复制到临时变量开始。然后用另一个元素覆盖该元素。然后用第三个元素覆盖第二个元素,依此类推,直到到达列/行的末尾。然后用临时变量覆盖最后一个元素。

向上旋转一列可以这样显示。红色数字是要执行的步骤。

enter image description here

下面是一些可以帮助您入门的代码。

#include <stdio.h>

// column  : The column to rotate
// rows    : The total number of rows
// columns : The total number of columns
void rotate_column_up(int column, int rows, int columns, int arr[][columns])
{
    int i;
    int temp = arr[0][column];
    for (i=1; i<rows; ++i)
    {
        arr[i-1][column] = arr[i][column];
    }
    arr[rows-1][column] = temp;
}

void rotate_column_down(int column, int rows, int columns, int arr[][columns])
{
    int i;
    int temp = arr[rows-1][column];
    for (i=rows-1; i>0; --i)
    {
        arr[i][column] = arr[i-1][column];
    }
    arr[0][column] = temp;
}

// row     : The row to rotate
// rows    : The total number of rows
// columns : The total number of columns
void rotate_row_left(int row, int rows, int columns, int arr[][columns])
{
    int i;
    int temp = arr[row][0];
    for (i=1; i<columns; ++i)
    {
         arr[row][i-1] = arr[row][i];
    }
    arr[row][columns-1] = temp;
}

void rotate_row_rigth(int row, int rows, int columns, int arr[][columns])
{
    int i;
    int temp = arr[row][columns-1];
    for (i=columns-1; i>0; --i)
    {
         arr[row][i] = arr[row][i-1];
    }
    arr[row][0] = temp;
}

void print_array(int columns, int rows, int arr[][columns])
{
    int i, j;
    for(j=0; j<rows; ++j)
    {
        for(i=0; i<columns; ++i)
        {
            printf("%d ", arr[j][i]);
        }
        printf("\n");
    }
        printf("\n");
}

int main(void) {
    int arr[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    printf("Original array\n");
    print_array(3, 3, arr);

    printf("Column 2 up\n");
    rotate_column_up(2, 3, 3, arr);
    print_array(3, 3, arr);

    printf("Row 2 rigth\n");
    rotate_row_rigth(2, 3, 3, arr);
    print_array(3, 3, arr);

    printf("Row 2 left\n");
    rotate_row_left(2, 3, 3, arr);
    print_array(3, 3, arr);

    printf("Column 2 down\n");
    rotate_column_down(2, 3, 3, arr);
    print_array(3, 3, arr);

    return 0;
}

输出:

Original array
1 2 3
4 5 6
7 8 9

Column 2 up
1 2 6
4 5 9
7 8 3

Row 2 rigth
1 2 6
4 5 9
3 7 8

Row 2 left
1 2 6
4 5 9
7 8 3

Column 2 down
1 2 3
4 5 6
7 8 9

关于c - C 中按列的多维操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46550204/

相关文章:

javascript - 使用jquery从外部url获取json数据

c - 找出字符串数组长度和每个字符串长度

c++ - Floyd 的最短路径算法 C++

c - Linux 内核中的进入和退出打印

c - opengl:无法绘制左墙

c - 如何在 php 扩展开发中调用 `callable` zval?

javascript - 如何比较 Javascript 数组?

c++ - 有没有办法替换 C++ 方法中的函数

algorithm - 线段树 : Lazy propagation

c++ - 计算 trampoline hook 的 JMP 指令地址