提问人:Charef Wissam 提问时间:1/6/2023 更新时间:1/7/2023 访问量:144
在列中查找最大元素,同时也是矩阵中行中的最小值
finding the max element in the columns while also being the min in the lines in a matrix
问:
编写一个程序,在大小为 L*C(L:行和 C:列)的矩阵中输入整数,然后确定并显示所有元素的值以及位置 (i,j),这些元素在其行上都是最小值,在列上是最大值。如果没有 Min-Max 不存在,我们将显示以下消息“矩阵不包含任何 Min-Max”。 示例 1: 1 2 3 4 5 6 7 8 9 13 15 16 表[4,1]=13 示例 2: 17 11 3 4 5 6 7 8 9 15 10 16 矩阵不包含最小值-最大值。 我尝试了这段代码,但它不起作用
int L , C , i , j ,maxc,minl ;
int Tab[20][20];
printf("Introduire le nombre des lignes du matrice (MAX 20): ");
scanf("%d",&L);
printf("Introduire le nombre des colonnes du matrice (MAX 20): ");
scanf("%d",&C);
for(i=0; i<L; i++)
{
for(j=0; j<C; j++)
{
printf("Donner l'element (%d,%d): ",i+1,j+1);
scanf("%d", &Tab[i][j]);
}
}
for(j=0 ;j<C;j++)
{
maxc=Tab[0][j];
for(i=0;i<L;i++)
{
minl=Tab[i][0];
if(Tab[i][j]>maxc && Tab[i][j]<minl )
{
printf("Tab[%d,%d]=%d ",i+1,j+1,Tab[i][j]);
}
else
{
printf("La matrice ne contient aucun Min-Max.");
}
}
}
return 0;
}
答:
我不确定当你只需要行和列时,为什么要创建一个大小为 20x20 的矩阵?您可以将其声明为L
C
int Tab[L][C];
就在您获得 和 的值之后,变量从 返回。L
C
scanf
至于算法,它不起作用,因为在使用 时,您将行索引固定为零(第一行):对于您要更新的每列,您都更新为存储在第一行中的相应行项值。
与 : 对于每一列和每一行,您更新到存储在第一列中的行索引项。
换言之,只能假定第一行的值,并且只能假定第一列的值。maxc=Tab[0][j]
maxc
minl=Tab[i][0]
maxc
i
maxc
minl
然后,在做 和 时不能匹配:例如,在第一次迭代时,() 粘在项目 [0,0] 上,而 go 在位置 [0,0], [1,0], [2,0], [3,0];然后,当 和 (内部循环第一次迭代的最后一步)时,你有 、 和 。if(Tab[i][j]>maxc && Tab[i][j]<minl)
maxc
minl
j=0
maxc
minl
j=0
i=3
maxc=[0,0]
minl=[3,0]
Tab[i][j]=[3][0]
相反,您必须在更新和:之前将最大值和最小值与行和列中的其他值进行比较,否则,它们可能只是随机匹配,具体取决于矩阵结构本身,当它们包含实际的最小值和最大值时,它甚至不会匹配。
此外,您应该重置并每次退出内部循环时,以避免处理前一行的最小值和最大值。maxc
minl
minl
maxc
至于下面的代码,它应该按预期工作,我只是做了相反的事情,通过搜索每行的最小值并将其索引保存在变量中(因为如果最小值不是最新的值,您将丢失索引);然后,当您拥有特定行的最小值时,您可以使用索引迭代该列,并搜索它是否存在更大的元素:如果存在,它会更新 .a
a
maxc
我还建议避免反转和索引以避免混淆(在您的代码中,您用于列和行,但在矩阵中插入用于列和行的值)。i
j
j
i
i
j
int L, C, min, max;
printf("Introduire le nombre des lignes du matrice:\n");
scanf("%d", &L);
printf("Introduire le nombre des colonnes du matrice:\n");
scanf("%d", &C);
int Tab[L][C]; //Declare it here with the right size
int i; //Index for iterating over rows
int j; //Index for iterating over columns
int a; //Index of the column with the minimum value (for each row)
int k; //Index for iterating the column that contains the min row value.
bool found = false;
for(i=0; i<L; i++){
for(j=0; j<C; j++){
printf("Donner l'element (%d, %d): ", i, j);
scanf("%d", &Tab[i][j]);
if(j==0){
min = max = Tab[i][j];
} else {
//Store the minimum and max values while user input them
if(Tab[i][j]<min) min = Tab[i][j];
if(Tab[i][j]>max) max = Tab[i][j];
}
}
}
for(i=0; i<L; i++){
//You need to reset 'maxc' and 'minl' for every row, otherwise you will compare it with the minimum and maximum values of the previous rows
int maxc = min-1;
int minl = max+1;
for(j=0; j<C; j++){
if(Tab[i][j]<minl){
minl = Tab[i][j];
a = j;
}
if(j==C-1){ //Here 'a' contains the row index of the minimum element of the row 'i'
for(k=0; k<L; k++){ //Iterating the column with 'a' index: we search for the maximum value in that column
if(Tab[k][a]>maxc) maxc = Tab[k][a];
if(k==L-1){ // Here 'a' contains the row index of the minimum element of the row 'i' (minl variable), and also here the maxc variable contains the maximum element of the row 'i'.
if(minl == maxc){
printf("Tab[%d,%d]=%d ", i, a, Tab[i][a]);
found = true;
}
}
}
}
}
}
if(!found) printf("La matrice ne contient aucun Min-Max.");
评论