我不确定这是否是正确的方法,但这是我到目前为止所做的:
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];
}
}
}
但是在此之后我现在每个col都有最大和最小的数字。如何在arr中替换它们?这甚至是正确的方式吗?
编辑: 我想从我的2d数组中的每一行中删除最大和最小的数字。例如,如果一行看起来像这样:3 5 1 3 11 2后记它应该看起来像3 5 3 2. 1和11已被删除。 我真的很感激任何帮助。谢谢!
编辑2: 我想从每行中删除最低和最高。很抱歉不清楚。
答案 0 :(得分:2)
您不仅要存储最大/最短数字的值,还要存储其索引。所以最后你可以修改它。
答案 1 :(得分: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;
}
}
答案 2 :(得分: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;
}
}
答案 3 :(得分:0)
在了解了你想要的东西之后,我正在给出一个新答案。希望这会有所帮助。
我正在取代min。最多每一行都为零。
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;
}
}
}