2012-04-03 18 views
0

私はかなりJavaに慣れていて、昨日Best Before puzzle from Spotifyを試しました。 他の解決策を確認することは役に立たず、どの入力が間違った答えを出すのか理解できません。 あなたはちょうど間違った答えにつながるコードを教えてください。私は自分自身でコードを修正できるはずです。Spotify puzzle Best Before

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

6月に減少させることができた

が30日あり、8月と7月の両方が31あなたの配列DPMは間違った方法で初期化されています。 2020年6月31日から2031年6月20日までは2020年6月20日です。

これが問題を解決することを願っています。

+0

良いキャッチ。 ....... –

+0

私はとても愚かな...ありがとう、たくさんのizo – geM

0

負の数値は処理しないことがあります。 2013年3月5日が有効です。また、プログラムは例外を伴って終了するので、無効な整数を正常に処理しません。 "as/5/12"を渡すと、 "as/5/12は違法です"と出力されます。 FYI、以下

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

ありがとうございました。 しかし、ifチェックを消去すると、最後のバリエーションが有効かどうかわかりません。 – geM

+0

あなたはまだ 'if(i == 6)'を行うことができます。 –

+0

okの負の数は固定されていますが、それでも私はまだ間違った答えを得ているようです。 (i <= 5 &&!checkDate(va [i] [0]、va [i] [1]、va [i] [2])) i ++; OutOfBounds Exceptionを与えるでしょう。私はi = 6で配列をチェックします。配列は0-5からだけです。 – geM