2

我必须写一个方法,该方法接收一个二维整数数组的参数。该方法返回整数和最大的行号。我只能使用递归!不允许循环!- 当然,我需要创建一个私有方法,将一行作为单个数组求和,然后我必须执行另一个比较行的私有方法,但它并没有真正起作用,因为我编写的方法仅适用于 1d数组,我需要比较二维数组中的一行..

感谢各种帮助..

我的一些代码:

private  int rowSum(int[] array, int index) {//the sum of an array(1d array)
        if (index == array.length)
            return 0;
        else
            return array[index] + rowSum(array, index + 1);
    }

**public int maxRow(int[][] a){------!!!---the problem...

    }**
4

5 回答 5

1

我会尝试使用参数从 main 调用递归方法:

  • 大批[][]
  • 当前列索引
  • 当前行索引
  • 当前行总和
  • 最高行索引
  • 最高行总和

我让它在这里工作,所以这绝对是可能的。大约有 15 行代码。祝你好运!

于 2013-01-10T07:36:40.020 回答
0

作为入门练习,尝试将rowSum函数的签名更改为:

int rowSum(int[] array, int index, int sumSoFar)

您应该能够编写函数,使递归部分的返回表达式只是一个递归调用:

if (...) // exit case
  return sumSoFar;
else
  return rowSum(...);

(现在这是一个尾递归实现。)

带着这种心态,想想你会怎么写:

int indexOfMaxValue(int[] array, int index, int maxValueSoFar, int maxIndexSoFar)

一旦你得到了这些,你应该能够将这两个概念结合在一起。

于 2013-01-10T07:41:08.997 回答
0

以下代码已准备好运行和测试:

public class RecursionSum {
    /* Sum a row until <code>index</code>. */
    private static int rowSum(int[] a, int index) {
        if (index == 0) { return a[0]; }
        // add the current element to the recursive sum
        return a[index] + rowSum(a, index - 1);
    }

    /* Sum a whole row. */
    private static int rowSum(int[] a) {
        return (a.length == 0) ? 0 : rowSum(a, a.length - 1);
    }

    /* Find the index of the array having the max sum until <code>index</code>. */
    private static int maxRow(int[][] a, int index){
        if (index == 0) { return 0; }
        // compare the current row's sum with the recursive max row's sum,
        // update index when it's a new winner
        int maxIndex = maxRow(a, index - 1);
        return (rowSum(a[maxIndex]) < rowSum(a[index])) ? index : maxIndex;
    }

    /* Find the index of the array having the max sum. */
    public static int maxRow(int[][] a){
        return a.length == 0 ? 0 : maxRow(a, a.length - 1);
    }

    /* very simple test */
    public static void main(String[] args) {
        int a1[][] = {};
        int a2[][] = {{1, 2, 3, 4}};
        int a3[][] = {{ 1, 2, 3, 4}, {8, 90}, {5, 6, 7}, {300, 4, 9}, {4, 6, 12}};
        System.out.println(maxRow(a1));
        System.out.println(maxRow(a2));
        System.out.println(maxRow(a3));
    }
}
于 2013-01-10T07:43:48.407 回答
0

代码:

public class MainClass {
    static  int contents[][] = { {1, 2 , 3, 4} , { 4, 5, 8, 7}, { 4, 2, 8, 7}, { 4, 5, 0, 7} };
    public static void main(String[] args) 
    {
        System.out.println(getIndexOfRowWithHighestSum(contents, 0, 0));
    }
    public static int getIndexOfRowWithHighestSum(int[][] twoDAray, int currentIndex,int indexWithHighestSum){
        if(currentIndex>=twoDAray.length){
            return indexWithHighestSum;
        }
        int sum1 = getSumOfArray(twoDAray[currentIndex], 0) ;
        int sum2 = getSumOfArray(twoDAray[indexWithHighestSum], 0);

        indexWithHighestSum = (sum1 > sum2)?currentIndex:indexWithHighestSum;

        return getIndexOfRowWithHighestSum(twoDAray, currentIndex+1,indexWithHighestSum);
    }
    public static int getSumOfArray(int[] array, int currentIndex){
        if(currentIndex>=array.length){
            return 0;
        }
        return array[currentIndex]+getSumOfArray(array,currentIndex+1);
    }
}
于 2013-01-10T08:20:45.490 回答
0

这是我想出的...

public static void main(String[] args)
{
    int[][] mat = { { 2, 6, 5, 4 }, { 3, 17, 2, 6 }, { 1, 3, 21, 0 } };

    System.out.println(maxRow(mat)); // 1

    int[][] mat1 = { { 2, 6, 5, 4 }, { 1, 3, 21, 0 }, { 3, 17, 2, 6 } };

    System.out.println(maxRow(mat1)); // 2
}

public static int maxRow(int[][] mat)
{
    return maxRow(mat, 0, 0, -1);
}

private static int maxRow(int[][] mat, int row, int maxSumInRow, int maxRowNo)
{
    if (row == mat.length)
        return maxRowNo;
            
    int sumRow = sumRow(mat, row, 0);
    
    if (sumRow > maxSumInRow)
    {
        maxSumInRow = sumRow;
        maxRowNo = row;
    }

    return maxRow(mat, row + 1, maxSumInRow, maxRowNo);
}

private static int sumRow(int[][] mat, int row, int col)
{
    if (col == mat[row].length)
        return 0;
    
    return mat[row][col] + sumRow(mat, row, col + 1);
}
于 2021-05-01T21:42:59.343 回答