简体   繁体   English

Java-三角形的递归和

[英]Java - Recursive Sum of a Triangle

I am trying to compute the sum of a triangle in an array where you add the max value of the three numbers below it (directly below, below and one to the left, and below and one to the right). 我正在尝试计算数组中三角形的总和,在其中将三个数字的最大值相加(正下方,正下方,一个向左,正下方和一个右向)。 I believe my problem is in where/how I am getting the number of rows in each triangle (numRows), due to the fact that it never updates past 3. I am not sure what I should do differently to get this to update as I thought it should update whenever the recursive method is called and is incremented down by 1 each time, but that is apparently not the case. 我相信我的问题是,在哪里/如何获得每个三角形中的行数(numRows),因为它永远不会更新到3以上。我不确定我应该做些什么来使它更新。认为每次调用递归方法时都应该更新它,并每次将其递减1,但显然并非如此。 Below is my code. 下面是我的代码。 Thank you all in advance! 谢谢大家!

/**
*
* @author Ra'kiir
*/
public class RecursiveTriangleSum {

public static int globalRows = 0;


/**
 * @param args the command line arguments
 * @throws java.io.FileNotFoundException
 */
public static void main(String[] args) throws FileNotFoundException {

    int[][] tempArray = new int[][]{
    {4}, //number of test cases
    {3}, //start: test case 1
    {1},
    {1, 2},
    {1, 2, 3},
    {5}, //start: test case 2
    {1},
    {1, 3},
    {3, 2, 1},
    {2, 1, 3, 4},
    {3, 5, 2, 4, 1},
    {1}, //start: test case 3
    {3},
    {6}, //start: test case 4
    {1},
    {2, 1},
    {1, 2, 3},
    {4, 3, 2, 1},
    {1, 2, 3, 4, 5},
    {3, 4, 5, 1, 2, 3}
    };

    int numTestCases = tempArray[0][0];
    globalRows++;  
    for (int z=0; z<numTestCases; z++) {

        int sum = recursiveTriSum(tempArray, globalRows, 0);
        System.out.println("The Sum is:  " + sum);
    }
} //End main method

public static int recursiveTriSum(int[][] array, int i, int j) { //i=row, j=column
//        System.out.println("Recursion Runs");

    int numRows = array[globalRows][0];
    if (i>numRows) {
//            System.out.println("IF");

        return 0;
    }
    else {
//            System.out.println("Else");
        int t1 = recursiveTriSum(array, i+1, j);
        int t2 = recursiveTriSum(array, i+1, j+1);
        int t3 = recursiveTriSum(array, i+1, j-1);

        int p1 = Math.max(t1, t2);
        int p2 = array[i][j] + Math.max(p1, t3);

        globalRows++;
        numRows--;
        return p2; 
    }

} //End recursiveTriSum method
} //End of Main Class

Iterate over each triangle as: 迭代每个三角形为:
- Get first row of triangle, initialize variable say sum = first element and another variable say index = 0. -获取三角形的第一行,初始化变量,说sum =第一个元素,初始化另一个变量,说index = 0。
- Move to next line in triangle, start from index and pick up next two elements. -移至三角形的下一行,从索引开始,然后拾取下两个元素。 If fist element is greater second, do not increment variable ie index otherwise increment it and add the greater value to sum. 如果第一个元素大于第二个,则不要增加变量,即索引,否则增加它,然后将较大的值相加。
eg 例如

If(array[index] > array[index+1])
       sum += array[index];
     else{
         sum += array[index+1];
         index++;
       }
and so on till last row of triangle. 依此类推,直到三角形的最后一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM