gpt4 book ai didi

c - 根据条件移动矩阵列

转载 作者:行者123 更新时间:2023-11-30 18:44:58 24 4
gpt4 key购买 nike

我正在尝试用 C 语言编写一个函数,该函数将 NxM 矩阵作为输入,如果它找到空列,则根据用户的选择将非空列移动到矩阵的左侧或右侧.空列可以是1,2或更多。

举个例子。假设以下矩阵:

矩阵元素是:

   1 2 3 4 5 6 7 8
------------------
1| 1 6 0 0 0 1 0 8
2| 1 3 0 3 0 1 0 0
3| 3 0 0 0 0 8 0 0
4| 0 0 0 2 6 0 0 4

正如您所看到的,只有第 3 列和第 7 列完全为空(全为 0)。我想要一个函数,如果用户选择 l (左侧),它将把非空列移动到左侧。所以它会返回:

矩阵元素是:

   1 2 3 4 5 6 7 8
------------------
1| 1 6 0 0 1 8 0 0
2| 1 3 3 0 1 0 0 0
3| 3 0 0 0 8 0 0 0
4| 0 0 2 6 0 4 0 0

如果用户选择r(向右),它将把非空列按原样移到右侧。因此函数将返回:

矩阵元素是:

   1 2 3 4 5 6 7 8
------------------
1| 0 0 1 6 0 0 1 8
2| 0 0 1 3 3 0 1 0
3| 0 0 3 0 0 0 8 0
4| 0 0 0 0 2 6 0 4

这是我的第一个方法:

   void PushL(int (*arr), int rows, int cols){ //function to move columns to left 
for(int i = 0; i < rows; i++)
for(int j = 0;j < cols; j++){
if(arr[i*cols+j] == 0)
for(int k = j + 1; k < cols; k++){
if(arr[i*cols+k] != 0){
arr[i*cols+j] = arr[i*cols+k];
arr[i*cols+k] = 0;
break;
}

}

}

}

最佳答案

由于缺少 {} 的问题,您的程序无法运行,因为您只考虑值为 0 的单元格或不独立于同一列中的其他单元格。

在决定是否移动之前,您需要知道所有列是否只包含 0

为此:

int onlyZero(int (*arr), int col, int rows, int cols)
{
const int sup = cols*rows;

for (int i = 0; i != sup; i += cols) {
if (arr[i + col] != 0)
return 0;
}

return 1;
}

如果列 col 仅包含 0,则返回 1,否则返回 0

复制其他列中的列:

void copyColumn(int (*arr), int fromCol, int toCol, int rows, int cols)
{
int sup = cols*rows;

for (int i = 0; i != sup; i += cols)
arr[i + toCol] = arr[i + fromCol];
}

并重置列:

void resetColumn(int (*arr), int col, int rows, int cols)
{
int sup = cols*rows;

for (int i = 0; i != sup; i += cols)
arr[i + col] = 0;
}

使用这些函数,即使这不是更快的方法:

void PushL(int (*arr), int rows, int cols)
{
int receiver = 0;

for (int col = 0; col != cols; ++col) {
if (!onlyZero(arr, col, rows, cols)) {
if (receiver != col)
copyColumn(arr, col, receiver, rows, cols);
receiver += 1;
}
}

while (receiver != cols)
resetColumn(arr, receiver++, rows, cols);
}

添加

void pr(int (*arr), int rows, int cols)
{
for (int row = 0; row != rows; ++row) {
for (int col = 0; col != cols; ++col) {
printf("%d ", arr[row*cols + col]);
}
putchar('\n');
}
putchar('\n');
}

int main()
{
int a[] = {
1, 6, 0, 0, 0, 1, 0, 8,
1, 3, 0, 3, 0, 1, 0, 0,
3, 0, 0, 0, 0, 8, 0, 0,
0, 0, 0, 2, 6, 0, 0, 4
};

pr(a, 4, 8);
PushL(a, 4, 8);
pr(a, 4, 8);
}

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra -Wall m.c
pi@raspberrypi:/tmp $ ./a.out
1 6 0 0 0 1 0 8
1 3 0 3 0 1 0 0
3 0 0 0 0 8 0 0
0 0 0 2 6 0 0 4

1 6 0 0 1 8 0 0
1 3 3 0 1 0 0 0
3 0 0 0 8 0 0 0
0 0 2 6 0 4 0 0

我让你做PushR,它与PushL非常接近

PushRPushL 的对称:

void PushR(int (*arr), int rows, int cols)
{
int receiver = cols - 1;

for (int col = cols-1; col != -1; --col) {
if (!onlyZero(arr, col, rows, cols)) {
if (receiver != col)
copyColumn(arr, col, receiver, rows, cols);
receiver -= 1;
}
}

while (receiver != -1)
resetColumn(arr, receiver--, rows, cols);
}

关于c - 根据条件移动矩阵列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56129086/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com