简体   繁体   English

找到列中的最大元素,同时也是矩阵中行中的最小元素

[英]finding the max element in the columns while also being the min in the lines in a matrix

Write a program to enter integers in a matrix of size L*C (L: row and C: column) then determine and display the value as well as the position (i,j) of all the elements which are both a minimum on their line and a maximum on their column.编写一个程序,在大小为 L*C(L:行和 C:列)的矩阵中输入整数,然后确定并显示所有元素的值以及 position (i,j),这两个元素都是它们的最小值行和他们列上的最大值。 If no Min-Max does not exist, we display the following message "The matrix does not contain any Min-Max.".如果没有 Min-Max 不存在,我们会显示以下消息“矩阵不包含任何 Min-Max。”。 Example 1: 1 2 3 4 5 6 7 8 9 13 15 16 Tab[4,1]=13 Example 2: 17 11 3 4 5 6 7 8 9 15 10 16 The matrix contains no 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。 i tried this code and it didnt work我试过这段代码,但没有用


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

I'm not sure why are you creating a matrix of size 20x20 when you only need L rows and C columns?当您只需要L行和C列时,我不确定为什么要创建大小为 20x20 的矩阵? You can just declare it as您可以将其声明为

int Tab[L][C];

just after you have the value of L and C variables returned from scanf .在您获得从scanf返回的LC变量的值之后。

As for the algorithm, it does not work because when using maxc=Tab[0][j] , you are fixing the row index to zero (first row): for every column you are updating maxc to the corresponding row items value stored in the first row.至于算法,它不起作用,因为在使用maxc=Tab[0][j]时,您将行索引固定为零(第一行):对于每一列,您都将maxc更新为存储在中的相应行项目值第一行。 The same for minl=Tab[i][0] : for every column, and for every row, you update maxc to the items of row index i which are stored in the first column. minl=Tab[i][0]相同:对于每一列和每一行,您将maxc更新为存储在第一列中的行索引i的项目。 In other words maxc can assume only values of the first row, and minl can assume only values of the first column.换句话说maxc只能取第一行的值,而minl只能取第一列的值。

Then, when doing if(Tab[i][j]>maxc && Tab[i][j]<minl) , maxc and minl can't match: as for example at the first iteration ( j=0 ) maxc is stuck to the item [0,0], while minl goes in the positions [0,0], [1,0], [2,0], [3,0];然后,在执行if(Tab[i][j]>maxc && Tab[i][j]<minl)时, maxcminl无法匹配:例如在第一次迭代时( j=0maxc卡住了到项目 [0,0],而minl进入位置 [0,0]、[1,0]、[2,0]、[3,0]; then, when j=0 and i=3 (last step of first iteration of the inner cycle), you have maxc=[0,0] , minl=[3,0] , and Tab[i][j]=[3][0] .然后,当j=0i=3 (内循环第一次迭代的最后一步)时,您有maxc=[0,0]minl=[3,0]Tab[i][j]=[3][0]

Instead you have to compare the maximum and minimum values against the other values in the rows and columns, before updating maxc and minl : otherwise, they could just randomly match, depending on the matrix structure itself, and it won't even match when they contains the real minimum and maximum values.相反,在更新maxcminl之前,您必须将最大值和最小值与行和列中的其他值进行比较:否则,它们可能只是随机匹配,具体取决于矩阵结构本身,甚至在它们匹配时也不会匹配包含实际的最小值和最大值。 Also, you should reset minl and maxc every time you exit from the inner cycle, in order to avoid dealing with minimum and maximum values of the previous row.此外,每次退出内循环时都应重置maxc minl以避免处理前一行的最小值和最大值。

As for the following code it should work as expected, I just did the inverse, by searching the minimum value for every row and saving its index in the a variable (because you will lose the index if the minimum value is not the latest one);至于下面的代码它应该按预期工作,我只是做了相反的事情,通过搜索每一行的最小值并将其索引保存a变量中(因为如果最小值不是最新的,你将丢失索引) ; then, when you have the minimum value for a specific row, you iterate that column with the index a and search if it exist an element which is bigger: in case it exists, it updates maxc .然后,当您具有特定行的最小值时,您使用索引a迭代该列并搜索它是否存在更大的元素:如果它存在,它会更新maxc

I would also suggest to avoid inverting i and j indexes in order to avoid confusion (in your code you was using j for columns and i for rows, but during the insertion of the values in the matrix you used i for columns and j for rows).我还建议避免反转ij索引以避免混淆(在您的代码中,您使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM