[英]finding the max element in the columns while also being the min in the lines in a matrix
编写一个程序,在大小为 L*C(L:行和 C:列)的矩阵中输入整数,然后确定并显示所有元素的值以及 position (i,j),这两个元素都是它们的最小值行和他们列上的最大值。 如果没有 Min-Max 不存在,我们会显示以下消息“矩阵不包含任何 Min-Max。”。 示例 1:1 2 3 4 5 6 7 8 9 13 15 16 Tab[4,1]=13 示例 2:17 11 3 4 5 6 7 8 9 15 10 16 矩阵不包含 Min-Max。 我试过这段代码,但没有用
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;
}
当您只需要L
行和C
列时,我不确定为什么要创建大小为 20x20 的矩阵? 您可以将其声明为
int Tab[L][C];
在您获得从scanf
返回的L
和C
变量的值之后。
至于算法,它不起作用,因为在使用maxc=Tab[0][j]
时,您将行索引固定为零(第一行):对于每一列,您都将maxc
更新为存储在中的相应行项目值第一行。 minl=Tab[i][0]
相同:对于每一列和每一行,您将maxc
更新为存储在第一列中的行索引i
的项目。 换句话说maxc
只能取第一行的值,而minl
只能取第一列的值。
然后,在执行if(Tab[i][j]>maxc && Tab[i][j]<minl)
时, maxc
和minl
无法匹配:例如在第一次迭代时( j=0
) maxc
卡住了到项目 [0,0],而minl
进入位置 [0,0]、[1,0]、[2,0]、[3,0]; 然后,当j=0
和i=3
(内循环第一次迭代的最后一步)时,您有maxc=[0,0]
、 minl=[3,0]
和Tab[i][j]=[3][0]
。
相反,在更新maxc
和minl
之前,您必须将最大值和最小值与行和列中的其他值进行比较:否则,它们可能只是随机匹配,具体取决于矩阵结构本身,甚至在它们匹配时也不会匹配包含实际的最小值和最大值。 此外,每次退出内循环时都应重置maxc
minl
以避免处理前一行的最小值和最大值。
至于下面的代码它应该按预期工作,我只是做了相反的事情,通过搜索每一行的最小值并将其索引保存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.");
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.