gpt4 book ai didi

c - 如何在C中检查矩阵的行或列是否具有相同的值

转载 作者:行者123 更新时间:2023-11-30 16:11:39 25 4
gpt4 key购买 nike

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

例如:

输入:

21 22 1

Output :

Yes

(because that matrix doesnt has a same value in a row or a column.)

Input 2 :

34 5 67 8 97 3 3

Output :

No 

(Because that matrix have same values in a row or column (3 & 3 and 7 & 7)

Input 3:

21 23 2

Output :

No 

(because that matrix have same value on column 1.)

Input 4

21 13 4

Output :

No

(because that matrix has same value in first row(1 1)

I have tried to do that, but some 'cases' still doesnt work. For example, i tried to include a count in my code but some of the count is not true.

example :input :

43 4 5 62 3 4 56 5 6 35 4 6 3

OUTPUT :

Nocount : 2

(it supposed to be 3 because it has the same value which are 6 (on row 3), 6 on column 3, and 3 on column 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;
}

最佳答案

正如我所见,您检查两个具有相同值的数字是否仅一列或一行不同:

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;
}

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

关于c - 如何在C中检查矩阵的行或列是否具有相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556469/

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