2012-04-03 27 views
0

我對Java很新,昨天試過Best Before puzzle from Spotify。我收到「錯誤答案」錯誤。 檢查其他解決方案沒有幫助,我無法確定哪個輸入提供了錯誤的答案。 最好你只是告訴我哪個代碼導致錯誤的答案。那麼我應該可以自己修改代碼。Spotify puzzle之前的最佳

import java.util.Scanner; 

public class Bestbefore { 

public static void main(String[] args) { 
    //Process Input String 
    Scanner scanner = new Scanner(System.in); 
    String dateString = scanner.nextLine(); 
    Integer a,b,c; 
    a=Integer.parseInt(dateString.substring(0,dateString.indexOf("/"))); 
    b=Integer.parseInt(dateString.substring(dateString.indexOf("/")+1, 
         dateString.lastIndexOf("/"))); 
    c=Integer.parseInt(dateString.substring(dateString.lastIndexOf("/")+1)); 
    int[][] va={   //All possible combinations 
      {a,b,c}, 
      {a,c,b}, 
      {b,a,c}, 
      {b,c,a}, 
      {c,a,b}, 
      {c,b,a} 
    }; 
    int i=0; 
    while (!checkDate(va[i][0], va[i][1], va[i][2]) 
      && i<5) // get the first valid date combination 
     i++; 
    //to prevent OutofBoundsException of the while loop 
    if (!checkDate(va[i][0], va[i][1], va[i][2]))   
      i++; 

    if (i==6)    
     System.out.println(dateString+" is illegal"); 
    else 
    { //compare the rest of the va-Array and save the position of the lowest Date in i 
     for (int k=i+1;k<6;k++)              
      if (checkDate(va[k][0], va[k][1], va[k][2])) 
      { 
       if (ageOfDate(va[k][0], va[k][1], va[k][2]) 
         < ageOfDate(va[i][0], va[i][1], va[i][2])) 
        i=k; 
      } 
      System.out.println(getDate(va[i][0], va[i][1], va[i][2])); 
    } 
} 
public static boolean checkDate(int day,int month, int year) 
{ 
    int[] dpm={31,28,31,30,31,31,30,31,30,31,30,31}; //Days per Month 
    if (year<2000) 
     year=year+2000; 
    if((year%4==0 && year%100!=0) || year%400==0) 
     dpm[1]=29;          //Leapyear correction 
    if (month==0 || month>12) 
     return false; 
    else 
     if (day==0 || day>dpm[month-1]) 
      return false; 
    else 
     return true; 
} 
public static int ageOfDate(int day,int month, int year) //to compare 2 dates 
{ 
    return ((year*10000)+(month*100)+day); 
} 
public static String getDate(int day,int month, int year) //for the Output 
{ 
    String smonth = String.valueOf(month); 
    String sday = String.valueOf(day); 
    if (year<2000) 
     year=year+2000; 
    if (month<10) 
     smonth="0"+smonth; 
    if (day<10) 
     sday="0"+sday; 
    return (year+"-"+smonth+"-"+sday); 
    } 
} 

回答

2

月有30天,月和7月有31你的陣列DPM以錯誤的方式進行初始化。所以這裏是你可能出錯的地方:31/06/20 - >最早可能是2031年6月20日,而不是2020年6月31日。

希望這可以解決你的問題。

+0

好的。 ....... – 2012-04-03 10:37:54

+0

我很愚蠢......非常感謝izo – geM 2012-04-03 11:10:14

0

可能是你不處理負數。 -3/5/2012將是有效的。而且,你的程序不會優雅地處理無效整數,因爲它會以異常退出。如果我通過「as/5/12」輸出應該是「as/5/12 is illegal」。

僅供參考,以下

while (!checkDate(va[i][0], va[i][1], va[i][2]) 
     && i<5) // get the first valid date combination 
    i++; 
//to prevent OutofBoundsException of the while loop 
if (!checkDate(va[i][0], va[i][1], va[i][2]))   
     i++; 

可以減少到

while (i <= 5 && !checkDate(va[i][0], va[i][1], va[i][2])) 
    i++; 
+0

謝謝生病下一次嘗試負數。 但是,如果我擦除如果檢查我不知道最後的變化是否有效。 – geM 2012-04-03 10:35:24

+0

你仍然可以做'if(i == 6)' – 2012-04-03 10:39:07

+0

確定負數是固定的,但這並不是所有的顯然,因爲我仍然得到錯誤的答案。 while(i <= 5 &&!checkDate(va [i] [0],va [i] [1],va [i] [2])) i ++;將給出一個OutOfBounds異常,我將檢查與i = 6和陣列只從0-5 – geM 2012-04-03 10:54:13