0

我不确定这是否是正确的方法,但这是我目前所拥有的:

int main(){
int foo[5][5]; //assume it is filled with postive numbers THERE ARE repeated numbers
int i, j; 
int a =0 ; //biggest
int b = 0; //smallest

for (i = 0; i<5; i++){
    for (j = 0; j<5; j++){
        if (a>foo[i][j])
            a = foo[i][j];
        if (b<foo[i][j])
            b = foo[i][j];
    }
}


}

但在此之后,现在我每个列都有最大和最小的数字。如何在 arr 中替换它们?这甚至是正确的方法吗?

编辑:我想从我的二维数组中的每一行中删除最大和最小的数字。例如,如果一行看起来像这样:3 5 1 3 11 2 afterwords 它应该看起来像 3 5 3 2。1 和 11 已被删除。我真的很感激任何帮助。谢谢!

编辑 2:我想从每一行中删除最低和最大的。很抱歉不清楚。

4

4 回答 4

2

您不仅要存储最大/最短数字的值,还要存储它的索引。所以最后你可以修改它。

于 2013-06-20T06:42:17.097 回答
0
int main(){
int foo[5][5]; //assume it is filled with postive numbers THERE ARE repeated numbers
int i, j; 
int a =foo[0][0] ; //biggest
int b = foo[0][0]; //smallest

for (i = 0; i<5; i++){
    for (j = 0; j<5; j++){
        if (a<foo[i][j])
            a = foo[i][j];
        if (b>foo[i][j])
            b = foo[i][j];
    }
}

for (i = 0; i<5; i++){
    for (j = 0; j<5; j++){
        if (a==foo[i][j])
            foo[i][j] = 0;
        if (b==foo[i][j])
            foo[i][j] = 0;
    }
}
于 2013-06-20T06:43:45.520 回答
0

我想这就是你需要的

int main(){
int foo[5][5]; //assume it is filled with positive numbers THERE ARE repeated numbers
int i, j, jmin, jmax; 


for (i = 0; i<5; i++)
{
    int a = foo[i][0]; //biggest
    int b = foo[i][0]; //smallest

    for (j = 0; j<5; j++)
    {
        if (foo[i][j] > a)
        {
           a = foo[i][j];
           jmax = j;
        }
        if (foo[i][j] < b)
        {
           b = foo[i][j];
           jmin = j;
        }
    }

    foo[i][jmax] = 0;
    foo[i][jmin] = 0;
}


}
于 2013-06-20T07:09:43.327 回答
0

在了解您想要什么之后,我提出了一个新的答案。希望这可以帮助。

我正在更换分钟。和最大。每一行都有一个零。

int main(){
int foo[5][5]; //assume it is filled with postive numbers THERE ARE repeated numbers
int i, j; 

for (i = 0; i<5; i++){
    int flagmin = 0, flagmax = 0;
    int rmin = f[i][0];
    int rmax = f[i][0];
    for (j = 0; j<5; j++){
        if (rmax < foo[i][j])
            rmax = foo[i][j];
        if (rmin > foo[i][j])
            rmin = foo[i][j];
    }
    for (j = 0; j<5; j++){
        if (rmax == foo[i][j] && flagmin = 0){
            flagmin = 1;
            foo[i][j] = 0;
        }
        if (rmin == foo[i][j] && flagmax = 0){
            flagmax = 1;
            foo[i][j] = 0;
        }
    }

}
于 2013-06-20T07:09:44.323 回答