【问题标题】:Spotify puzzle Best BeforeSpotify 拼图
【发布时间】:2012-04-03 10:15:15
【问题描述】:

我对 Java 还很陌生,昨天尝试了 Best Before puzzle from Spotify。我在发送时收到“错误答案”错误。 检查其他解决方案没有帮助,我无法弄清楚哪个输入给出了错误的答案。 充其量你只会告诉我哪个代码导致错误的答案。那我应该可以自己更正代码了。

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

【问题讨论】:

    标签: java puzzle spotify


    【解决方案1】:

    6 月有 30 天,8 月和 7 月都有 31 天。您的数组 dpm 以错误的方式初始化。因此,您可能会出错:31/06/20 -> 最早可能是 2031 年 6 月 20 日,而不是 2020 年 6 月 31 日。

    希望这能解决您的问题。

    【讨论】:

      【解决方案2】:

      可能是您不处理负数。 -3/5/2012 有效。此外,您的程序不会优雅地处理无效整数,因为它会以异常退出。如果我通过“as/5/12”输出应该是“as/5/12 是非法的”。

      仅供参考,以下

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

      【讨论】:

      • 谢谢我接下来试试负数。但如果我删除 if 检查,我不知道最后一个变化是否有效。
      • 你仍然可以做if(i==6)
      • 好的负数是固定的,但这显然不是因为我仍然得到错误的答案。而 (i
      • 这就是为什么i &lt;= 5这样checkDatei=6时不会被执行。您确实意识到 &amp;&amp; 是一个简短的 AND,如果第一个失败,它将不会评估条件的第二部分。
      • 啊好吧不知道。谢谢!
      猜你喜欢
      • 2011-11-14
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多