Java 查找最大的行和列时,解决方案不一致
逻辑错误在哪里?。。有时解决方案是正确的,有时则不是。假设程序计算和最大的行和列。例如: 1 01 0 01 0 01 0 那么输出将是: 最大行=0 最大列=2//因为计数从0开始 这就是我所拥有的:Java 查找最大的行和列时,解决方案不一致,java,sorting,Java,Sorting,逻辑错误在哪里?。。有时解决方案是正确的,有时则不是。假设程序计算和最大的行和列。例如: 1 01 0 01 0 01 0 那么输出将是: 最大行=0 最大列=2//因为计数从0开始 这就是我所拥有的: import java.util.Random; public class LargestRowAndColumn { public static void main(String[] args) { Random f = new Random();
import java.util.Random;
public class LargestRowAndColumn {
public static void main(String[] args) {
Random f = new Random();
int[][] m = new int[4][4];
for (int i = 0; i < m.length; i++) {
for (int j = 0;j < m[0].length; j++) {
m[i][j] = f.nextInt(2);
}
}
for (int i = 0; i < m.length; i++) {
for (int j = 0;j < m[0].length; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}
System.out.println("The largest row is index: " + computeRow(m));
System.out.println("The largest column is index: " + computeColumn(m));
}
public static int computeRow(int[][] m) {
int[] count = new int[m.length];
int sum;
for (int i = 0; i < 4; i++) {
sum = 0;
for (int j = 0; j < 4; j++) {
sum = sum + m[i][j];
}
count[i] = sum;
}
int maxIndex = 0;
for (int i = 0; i < i + 1; i++) {
for (int j = count.length - 1; j >= i; j--) {
if (count[i] < count[j]) {
maxIndex = j;
break;
}
}
}
return maxIndex;
}
public static int computeColumn(int[][] m) {
int[] count = new int[m.length];
int sum = 0;
for (int i = 0; i < 4; i++) {
sum = 0;
for (int j = 0; j < 4; j++) {
sum = sum + m[j][i];
}
count[i] = sum;
}
int maxIndex = 0;
for (int i = 0; i < i + 1; i++) {
for (int j = count.length - 1; j >= i; j--) {
if (count[i] < count[j]) {
maxIndex = j;
break;
}
}
}
return maxIndex;
}
}
import java.util.Random;
公共类最大行和列{
公共静态void main(字符串[]args){
随机f=新随机();
int[]m=新int[4][4];
对于(int i=0;i=i;j--){
如果(计数[i]<计数[j]){
maxIndex=j;
打破
}
}
}
返回最大索引;
}
公共静态int computeColumn(int[]m){
int[]计数=新的int[m.长度];
整数和=0;
对于(int i=0;i<4;i++){
总和=0;
对于(int j=0;j<4;j++){
sum=sum+m[j][i];
}
计数[i]=总和;
}
int maxIndex=0;
对于(int i=0;i=i;j--){
如果(计数[i]<计数[j]){
maxIndex=j;
打破
}
}
}
返回最大索引;
}
}
您的maxIndex嵌套循环太复杂。它应该是一个单循环,用循环中的当前项检查到目前为止看到的当前最大值。大概是这样的:
int maxIndex = 0;
for (int i = 1; i < count.length; i++) {
if (count[i] > count[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;
intmaxindex=0;
for(int i=1;icount[maxIndex]){
maxIndex=i;
}
}
返回最大索引;
为什么你让工作变得困难。做2个循环,1个用于计算和最大的行,1个用于计算和较大的行
您不需要整数数组count[i]
。在您的示例中,计算总和最大的行,不需要知道for循环完成后每行的总和,因此可以使用简单的int bigRow
int bigRow = 1, sumRow = 0;
// We assume that 1st row is the biggest
// Calculate the sumRow
for (int j=0;j<n;j++)
sumRow = sumRow + m[i][j] ;
// At this moment our maximum is row 1 with its sum.
// Now we compare it with the rest of the rows
// If another row is bigger, we set him as the biggest row
for ( int i=1;i<n;i++) // We start with row 2 as we calculated the 1st row
{ int auxRow = 0;
for (int j=0;j<m;j++)
{ auxRow = auxRow + m[i][j] ; }
if (auxRow > sumRow ) { auxRow=sumRow ; bigRow = i;}
}
如果您还有其他问题,请告诉我。您的代码是正确的,但是
for (int i = 0; i < m.length; i++) {
for (int j = 0;j < m[0].length; j++) {
m[i][j] = f.nextInt(2);
}
}
for (int i = 0; i < m.length; i++) {
for (int j = 0;j < m[0].length; j++) {
System.out.print(m[i][j] + " ");
}
for(int i=0;i
因为有两个循环:
您正在创建两个随机二维数组,而不是一个。
其中一个正在打印,另一个未打印,但用于所需的索引值,请执行以下操作:
System.out.print("Index" + "\t0"+"\t1"+"\t2"+"\t3" +"\n");
System.out.print("--------------------------------------------\n");
for (int i = 0; i < m.length; i++) {
System.out.print(i+ "|\t");
for (int j = 0;j < m[0].length; j++) {
m[i][j] = f.nextInt(101);
System.out.print(m[i][j] + " \t");
}
System.out.println();
}
System.out.print(“索引”+“\t0”+“\t1”+“\t2”+“\t3”+”\n”);
System.out.print(“------------------------------------------------------------\n”);
对于(int i=0;i
这也会打印索引,这可能会帮助您那么到底是什么问题呢?有时它不会打印正确的行和列(总和最大的行和列)
System.out.print("Index" + "\t0"+"\t1"+"\t2"+"\t3" +"\n");
System.out.print("--------------------------------------------\n");
for (int i = 0; i < m.length; i++) {
System.out.print(i+ "|\t");
for (int j = 0;j < m[0].length; j++) {
m[i][j] = f.nextInt(101);
System.out.print(m[i][j] + " \t");
}
System.out.println();
}