2012-04-03 4 views
0

Je suis assez nouveau à Java et j'ai essayé le Best Before puzzle from Spotify hier. Je reçois l'erreur "Mauvaise réponse" quand je l'envoie. Recherche d'autres solutions n'a pas aidé et je ne peux pas comprendre quelle entrée donne une mauvaise réponse. Au mieux, vous me direz simplement quel code conduit à la mauvaise réponse. Je devrais être capable de corriger le code moi-même alors.Spotify puzzle Meilleur avant

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

Répondre

2

Juin a 30 jours, à la fois Août et Juillet ont 31. Votre dpm tableau est initialisé d'une manière erronée. Donc, voici où vous pouvez vous tromper: 31/06/20 -> le plus tôt possible est le 20 juin 2031 au lieu du 31 juin 2020.

Espérons que cela résout votre problème.

+0

Bonne prise. ....... –

+0

Im tellement stupide ... merci beaucoup izo – geM

0

Peut-être que vous ne gérez pas les nombres négatifs. -3/5/2012 serait valide. De plus, votre programme ne gère pas correctement les entiers invalides car il sortira avec une exception. Si je passe "comme/5/12" la sortie devrait être "comme/5/12 est illégal".

Pour votre information, ce qui suit

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

pourrait être réduite à

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

merci d'essayer les numéros négatifs suivants. Mais si j'efface la case if, je ne sais pas si la dernière variation est valide ou non. – geM

+0

vous pouvez toujours faire le 'si (i == 6)' –

+0

ok les nombres négatifs sont fixés mais ce n'était pas tout apparemment comme je reçois toujours la réponse erronée. while (i <= 5 &&! checkDate (va [i] [0], va [i] [1], va [i] [2])) i ++; va donner une Exception OutOfBounds je vérifie avec i = 6 et le tableau ne va que de 0-5 – geM