3 回答
TA贡献1865条经验 获得超7个赞
for (int i = 0; i < 10; i++) {
int oneCountRow = 0;
int oneCountColumn = 0;
for (int j = 0; j < 10; j++) {
if (array[i][j] == 1) {
oneCountRow++;
}
if (array[j][i] == 1) {
oneCountColumn++;
}
}
System.out.println("Row " + (i + 1) + ": " + (oneCountRow > 5));
System.out.println("Column " + (i + 1) + ": " + (oneCountColumn> 5));
}
或免费尺寸:
int maxLenght = 0;
for (int i = 0; i < array.length; i++) {
maxLenght = Math.max(maxLenght, array[i].length);
}
maxLenght = Math.max(maxLenght, array.length);
for (int i = 0; i < maxLenght; i++) {
int oneCountRow = 0;
int oneCountColumn = 0;
for (int j = 0; j < maxLenght; j++) {
if (j < array[i].length && i < array.length && array[i][j] == 1) {
oneCountRow++;
}
if (i < array[j].length && j < array.length && array[j][i] == 1) {
oneCountColumn++;
}
System.out.println("Row " + (i + 1) + ": " + (oneCountRow > 5));
System.out.println("Column " + (i + 1) + ": " + (oneCountColumn> 5));
}
TA贡献1836条经验 获得超4个赞
以下是我对解决方案的看法:
您可以创建一个大小为“计数数组”的“计数数组”,max(n, m) * 2
其中n
和m
是矩阵的维度,并使用0
s对其进行初始化。该2
代表它的数列或行是否(如指数0
代表列和1
行)。然后,当您找到 a 1
(对于列和行)时,增加该数组中的代表值。找到正确的索引只是一些模块化算法的问题。
添加回答
举报