【问题标题】:Can not get precise results for the hourglass algorithm沙漏算法无法得到精确的结果
【发布时间】:2016-11-23 11:44:56
【问题描述】:

这里是沙漏问题定义的链接: https://www.hackerrank.com/challenges/30-2d-arrays

我写了以下程序:

package day11;

import java.util.Scanner;

public class Solution {

    public static void main(String ... args){

        Scanner scan = new Scanner(System.in);

        int[][] arr = new int[6][6];

        int maxHourGlassValue = 0;
        int temp = 0;
        int currMax = 0;

        int k = 0, l = 0;

        for(int i = 0 ; i < 6 ; i++){
            for(int j = 0 ; j < 6 ; j++){
                arr[i][j] = scan.nextInt();
            }
        }

        for(int i = 1 ; i < 5 ; i++){
            for(int j = 1 ; j < 5 ; j++){
                    if(maxHourGlassValue < currMax){
                    maxHourGlassValue = currMax;
                }
            }


        }

        System.out.println(maxHourGlassValue);

    }

}

我只能运行 8 个给定测试用例中的 6 个。怎么可能出错???

【问题讨论】:

  • 可能出了什么问题? 很多。人们一直在编码错误。这就是为什么调试是整个学术研究领域的原因。
  • 到目前为止你还没有写任何逻辑。它甚至没有为示例测试用例运行,那么它将如何运行 8 个中的 6 个。无论如何,我已经为您的沙漏问题回答了完整的测试代码。

标签: java arrays algorithm array-algorithms


【解决方案1】:

试试这个代码,我没有写那个代码,我只是复制它并从here修改它

import java.util.Scanner;
public class Test {

    public static void main(String ... args){
        Scanner scan = new Scanner(System.in);
        int size = 6;
        int[][]  m = new int[size][size];

      //numbers input
        for(int i=0; i<size; i++)
        {
          for(int j=0; j<size; j++)
           {
            m[i][j] = scan.nextInt();
           }
        } 

        int temp = 0, MaxSum = -99999;

        for (int i=0; i<size; ++i) {
            for (int j=0; j<size; ++j) {
                if (j+2 < size && i+2 < size) {
                    temp = m[i][j] + m[i][j+1] + m[i][j+2] + m[i+1][j+1] + m[i+2][j] + m[i+2][j+1] + m[i+2][j+2];
                    if (temp >= MaxSum) {
                        MaxSum = temp;
                    }
                }
            }
        }   
        System.out.println(MaxSum);

    }

}

【讨论】:

  • 如果你也能提供一些描述会更好。您可以将temp &gt; MaxSum 条件而不是temp &gt;= MaxSum 放在for 循环内的`if` 中。
【解决方案2】:

下面是hourglass problem的所有测试用例成功运行的代码。

public static void main(String[] args) {
    try (Scanner scan = new Scanner(System.in)) {
        int[][] arr = new int[6][6];

        int maxHourGlassValue = -63;//Assigning (-9*7=)-63 which is the minimum possible value of "hourglass sum".

        //Reading inputs.
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                arr[i][j] = scan.nextInt();
            }
        }

        //Logic.
        /**
         * Index of both i and j will run from 1 to 4 (one less than n-1 where n = 6)
         * So for each i and j iteration calculating the sum of hourglass.
         */
        int iHGValueTemp = 0;
        for (int i = 1; i < 5; i++) {
            for (int j = 1; j < 5; j++) {
                iHGValueTemp = arr[i][j] + /*Main element*/
                        arr[i - 1][j - 1] + arr[i - 1][j] + arr[i - 1][j + 1]+ /*Top three elements of main element.*/
                        arr[i + 1][j - 1] + arr[i + 1][j] + arr[i + 1][j + 1]; /*Bottom three elements of main element.*/
                if (iHGValueTemp > maxHourGlassValue) {
                    maxHourGlassValue = iHGValueTemp;
                }
            }

        }

        //Output.
        System.out.println(maxHourGlassValue);
    }
}

我只在 cmets 的代码中编写了描述。如有疑问,请参阅并与我讨论。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-01-25
    相关资源
    最近更新 更多