最好的拼图

时间:2012-02-05 23:27:56

标签: java algorithm

  

可能重复:
  Spotify puzzle problem

正在研究这个难题,因为我是Java的新手,想学习它。所以,我一直在为Spotify解决这个难题。但每次我提交它。它说错了答案。我很确定它的权利,但任何人都可以看到我所缺少的。这就是问题。 http://www.spotify.com/us/jobs/tech/best-before/

这是我的解决方案

       import java.io.*;
import java.util.Arrays;

public class best_before {  
    static boolean next;
    static String month, day, year;

    public static void main(String[] args) throws IOException{

        //Setup to grab the input
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);        
        String input = "";  
        input = br.readLine();

        //Spilt the input to grab each integer entered by user
            String bits = input;
            String[] tokens = bits.split("/");

            int a_value1 = Integer.parseInt(tokens[0]);
            int b_value1 = Integer.parseInt(tokens[1]);
            int c_value1 = Integer.parseInt(tokens[2]);

            //Sort the array in order from lowest to highest
            int[] int_array = new int[] {a_value1, b_value1, c_value1};
            Arrays.sort(int_array);

            int a_value = int_array[0];
            int b_value = int_array[1];
            int c_value = int_array[2];

            year = Integer.toString(a_value);
            month = Integer.toString(b_value);
            day = Integer.toString(c_value);

            //Check the integers entered to put them in the right order
            int check_days = Integer.parseInt(day);
            int check_month = Integer.parseInt(month);
            int check_year = Integer.parseInt(year);

            //Check to make sure none of the values are a negative integer
            if(check_days < 0){
                System.out.println(input + " is illegal");
                System.exit(0);
            }
            else if(check_month < 0){
                System.out.println(input + " is illegal");
                System.exit(0);
            }
            else if(check_year < 0){
                System.out.println(input + " is illegal");
                System.exit(0);
            }

            //Will only change the values around if the highest date in the array is bigger than 31
            if(check_days > 31){

                //Only reorganize if year if larger than month
                if(check_month > check_year){
                    month = Integer.toString(check_year);
                }
                //Otherwise just keep month at its current value
                else{
                    month = Integer.toString(check_month);
                }
                //Change date and year around since one is bigger than the other
                    year = Integer.toString(check_days);
                    day = Integer.toString(check_month);

            }
            else if(check_year == 0){
                if(check_month < check_days){
                    month = Integer.toString(check_month);
                }
                else{
                    month = Integer.toString(check_days);
                }
            }

            //Get the length so I can zero pad the numbers
            int length_year = year.length();
            int length_month = month.length();
            int length_day = day.length();

            //Doing my zero pad thing right here
            if(length_year == 1){
                year = "200" + year;
            }
            else if(length_year == 2){
                year = "20" + year;
            }
            else if(length_year == 3){
                year = "2" + year;
            }
            if(length_month == 1){
                month = "0" + month;
            }
            if(length_day == 1){
                day = "0" + day;
            }

            //A last check to make sure everything is Kosher
            int last_check = Integer.parseInt(year);
            int last_check_month = Integer.parseInt(month);
            int last_check_day = Integer.parseInt(day);

            //Checking to see if it is a leap year. Is the year Divisible by 4?
            if (last_check % 4 == 0) {
                // Is the year Divisible by 4 but not 100?
                if (last_check % 100 != 0) {
                    next = true;
                }
                // Is the year Divisible by 4 and 100 and 400?
                else if (last_check % 400 == 0) {
                    next = true;
                }
                // It is Divisible by 4 and 100 but not 400!
                else {
                    next = false;
                }
            }
            // It is not divisible by 4.
            else {
                next = false;
            }

            //Check to make sure the date is legal and valid :)
            if(last_check > 2999 || last_check < 2000)
            {
                //Date must be between 2000 and 2999 inclusive
                System.out.println(input + " is illegal");
            }
            else if(last_check_month > 12)
            {
                //Date month must be less than 12
                System.out.println(input + " is illegal");
            }
            else if(last_check_day > 31)
            {
                //Date day must be less than 31
                System.out.println(input + " is illegal");
            }
            else if(next == false && last_check_day > 28 && last_check_month == 2){
                //if next is false and the day is greater than 28 and its not a leap year something is wrong
                System.out.println(input + " is illegal");
            }
            else if(next == true && last_check_day > 29){
                //if next is true and the day is greater than 29 and it is a leap year something is wrongs
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 4 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 6 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 9 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check_month == 11 && last_check_day > 30){
                System.out.println(input + " is illegal");
            }
            else if(last_check == 2000){
                //Check to make sure there are no other days that are zero too because you cant have two zeros
                if(last_check_day == 0 || last_check_month == 0){
                    System.out.println(input + " is illegal");
                }
                else{
                    System.out.print(year + "-" + month + "-" + day);
                }
            }
            else{
                System.out.print(year + "-" + month + "-" + day);
            }       
        }

    }

1 个答案:

答案 0 :(得分:2)

您的程序针对输入31/9/73失败,返回2073-09-31而不是31/9/73 is illegal。由于9月只有30天,你的程序应检查9月(以及其他几个月),并输出错误。

在更新版本中,您的程序仍然无法输入1/2/31;它应该给2031-1-2

顺便说一句,这些谜题背后的主要目的不是解决它们,而是解决它们优雅。尝试减少变量的数量,避免重复的行等。