二维数组中的每个元素都被认为是一块巧克力。除了每行和每列的最大巧克力之外,我需要选择最大数量的巧克力。我做了一个程序,但它没有为下面提到的例子产生正确的结果。
1 4 0 5 2 | 4
2 1 2 0 1 | 1
0 2 3 4 4 | 3
0 3 0 3 1 | 1
1 2 2 1 1 | 1
列: 1+3+2+4+2=12
行: 4+1+3+1+1=10
全部的: 12+10=22
输出应该是 22,但我下面附加程序的输出是 20。
public int calc(int[][] grid, int rows, int columns){
int[][] check = new int[rows][columns];
int max, pos;
for(int i=0; i<rows; i++){
max = grid[i][0];
pos = 0;
for(int j=0; j<columns; j++){
if(grid[i][j] >= max){
max = grid[i][j];
pos = j;
}
}
check[i][pos] = 1;
}
for(int j=0; j<columns; j++){
max = grid[0][j];
pos = 0;
for(int i=0; i<rows; i++){
if(grid[i][j] >= max){
max = grid[i][j];
pos = i;
}
}
check[pos][j] = 1;
}
int total = 0;
for(int i=0; i<rows; i++){
for(int j=0; j<columns; j++){
if(check[i][j]!=1){
total += grid[i][j];
}
}
}
return total;
}
汪汪一只猫
相关分类