找到最大的行和列,解决方案是不一致的

时间:2015-02-20 11:08:02

标签: java sorting

逻辑错误在哪里?有时解决方案是正确的,有时却不是。该程序假设计算具有最大总和的行和具有最大总和的列。例如:

1 1 1 1

0 0 1 0

0 0 1 0

0 0 1 0

然后输出将是: 最大行= 0 最大列= 2 //因为计数从0开始

这就是我所拥有的:

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;
    }
}

3 个答案:

答案 0 :(得分:1)

你的maxIndex嵌套循环太复杂了。它应该是一个单循环,用循环中的当前项检查当前看到的当前最大值。像这样:

    int maxIndex = 0;

    for (int i = 1; i < count.length; i++) {
        if (count[i] > count[maxIndex]) {
            maxIndex = i;
        }
    }
    return maxIndex;

答案 1 :(得分:1)

您的代码是正确的,但

  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.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();
    }

这也将打印索引,这可能会帮助您

答案 2 :(得分:0)

为什么你的工作很困难。制作2个循环,1个用于计算具有最大总和的行,1个用于计算具有较大总和的行。

您不需要int数组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;}
   }

对行做同样的事。

int bigLine = 1, sumLine = 0 ;

如果您有其他问题,请告诉我。