2016-11-23 82 views
1

這裏是鏈接的沙漏問題的定義: 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); 

    } 

} 

我只能跑6出8給出測試用例。什麼可能會出錯?

+2

*什麼可能會出錯?*很多。人們總是編碼錯誤。這就是調試是整個學術研究領域的原因。 –

+0

到目前爲止您還沒有編寫任何邏輯。它甚至沒有運行示例測試用例,那麼它將如何運行,滿足8個。無論如何,我已經爲您的沙漏問題回答了一個完整的測試代碼。 –

回答

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

    } 

} 
+0

如果你可以提供一些描述,那會更好。你可以把'temp> MaxSum'條件放在'for'循環裏'if'裏,而不是'temp> = MaxSum'。 –

0

修改了它下面是一個將成功運行的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); 
    } 
} 

我在評論中的代碼中只寫了描述。如果有任何疑問,請參考並與我討論。