【问题标题】:how to check if a matrix has the same value in a row or column in C如何检查矩阵在C中的行或列中是否具有相同的值
【发布时间】:2019-10-25 10:09:08
【问题描述】:

我有一个家庭作业问题。它要求我们根据用户的输入制作一个矩阵。例如:如果用户输入 4,则矩阵将为 4 X 4。之后,程序将检查矩阵在行或列中是否具有相同的值。它会给出是或否输出。

例如:

输入:

2 1 2 2 1

输出:

是的

(因为该矩阵在一行或一列中没有相同的值。)

输入 2:

3 4 5 6 7 8 9 7 3 3

输出:

(因为该矩阵在一行或一列中具有相同的值(3 & 3 和 7 & 7)

输入 3:

2 1 2 3 2

输出:

(因为该矩阵在第 1 列具有相同的值。)

输入 4

2 1 1 3 4

输出:

(因为该矩阵在第一行具有相同的值(1 1)

我已经尝试过这样做,但有些“案例”仍然不起作用。例如,我试图在我的代码中包含一个计数,但有些计数不正确。

示例: 输入:

4 3 4 5 6 2 3 4 5 6 5 6 3 5 4 6 3

输出:

不 计数:2

(它应该是 3,因为它具有相同的值,即 6(在第 3 行)、6 在第 3 列和 3 在第 4 列。)

#include "stdio.h"

int main()
{

    int matrix[500][500];
    int testcase;
    int count = 0;
    scanf("%d",&testcase); getchar();

    for(unsigned i = 0; i < testcase; i++) {
        for(unsigned j = 0; j < testcase; j++) {
            scanf("%d",&matrix[i][j]); getchar();
        }
    }
    // printf("[0,0] = %c",matrix[0][0]);
    // printf("\n[0,1] = %c",matrix[0][1]);
    // printf("\n[1,0] = %c",matrix[1][0]);
    // printf("\n[1,1] = %c",matrix[1][1]);

    for(unsigned i = 0; i < testcase; i++) {
        for(unsigned j = 0; j < testcase; j++) {
            if(matrix[i][j] == matrix[i][j+1]) {
                count = count + 1;
            }
            else if(matrix[i][j] == matrix[i+1][j]) {
                count = count + 1;
            }

        }
    }
    if(count > 0) {
        printf("No\n");
    } else{
        printf("Yes\n");
    }

    printf("Count : %d\n",count );
    getchar();
    return 0;
}

【问题讨论】:

  • 您的测试将超出数组初始化部分的范围。
  • 应该有两组测试循环:一组检查每一行,一组检查每一列。另外:您可以删除每个getchar(),因为scanf 将过滤空格(在此用法中)。

标签: c matrix


【解决方案1】:

正如我所见,您在这里检查两个相同值的数字是否仅相差一列或一行:

if(matrix[i][j] == matrix[i][j+1]) {
      count = count + 1;
}
else if(matrix[i][j] == matrix[i+1][j]) {
      count = count + 1;
}

我认为您可能需要一个临时变量,以便您可以分别扫描每一行然后每一列,例如:

temp = matrix[i][j];
if(checkRow(temp, i, j, matrix, testcase) == true) count++;
if(checkColumn(temp, i, j, matrix, testcase) == true) count++;

checkRow 是这样的:

bool checkRow(int temp, int row, int col, int matrix[][500], int size)
{
   for(int i=col; i < size;)
   {
      if(temp == matrix[row][i]) return true;
   }

    return false;
}

您将分别构建 checkColumn 函数。

编辑: 既然你告诉我你还没有学会如何使用函数,这将是你的最终程序。它有效,我可能会建议最终的测试用例应该输出“count = 4”,因为您可能会错过一个案例。 代码如下:

#include "stdio.h"
int main()
{

    int matrix[500][500];
    int testcase;
    int count = 0;
    scanf("%d",&testcase); getchar();
    int temp;
    for(unsigned i = 0; i < testcase; i++) {
        for(unsigned j = 0; j < testcase; j++) {
            scanf("%d",&matrix[i][j]); getchar();
        }
    }
    // printf("[0,0] = %c",matrix[0][0]);
    // printf("\n[0,1] = %c",matrix[0][1]);
    // printf("\n[1,0] = %c",matrix[1][0]);
    // printf("\n[1,1] = %c",matrix[1][1]);

    for(unsigned i = 0; i < testcase; i++) {
        for(unsigned j = 0; j < testcase; j++) {
            temp = matrix[i][j];
            //Scan current row
            for(unsigned k = j+1; k < testcase; k++)
            {
                if(temp == matrix[i][k])
                {
                    count++;
                    break;
                }
            }
            //Scan current column
            for(unsigned k = i+1; k < testcase; k++)
            {
                if(temp == matrix[k][j])
                {
                    count ++;
                    break;
                }
            }
        }
    }
    if(count > 0) {
        printf("No\n");
    } else{
        printf("Yes\n");
    }

    printf("Count : %d\n",count );
    getchar();
    return 0;
}

我建议,在复制代码之前,您必须了解其背后的算法。这是简单而蛮力的想法。

【讨论】:

  • 传递 2 星参数的问题在于它不是二维数组,编译器无法判断它的维度是多少。您可以将两颗星用于指针数组。
  • 当然,您必须创建一个循环来扫描每个“matrix[i][j]”元素所属的行和列。如果您发现重复增加计数。请注意,当您分别扫描列或行时,您必须从当前 i 或 j 开始扫描,希望对您有所帮助。如果您难以理解我的建议,我稍后会提供更完整的答案。祝你好运!
猜你喜欢
  • 2016-04-08
  • 2013-01-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-27
  • 2012-04-11
  • 1970-01-01
  • 1970-01-01
  • 2019-03-09
相关资源
最近更新 更多