usaco:星期五十三我的逻辑有什么问题?

时间:2013-03-31 03:50:24

标签: java

这个问题要求计算一周中每一天的第13个数字。这是我的代码。

class CopyOffriday {
public static void main(String[] args) throws IOException {
        BufferedReader f = new BufferedReader(new FileReader("friday.txt"));

        int n1=Integer.parseInt(f.readLine());
        int[] counter=new int[7];

        int N=1900+n1-1;
        int position=1; //first 13th is a Saturday


        for(int i=1900; i<=N;i++){
            for(int month=1; month<=12;month++){
                if((i==1900)&&(month==1)) counter[position-1]++; 
                else if((i==N)&&(month==11)){
                    position+=2;
                    position%=7;
                    counter[position-1]++; 
                     System.out.println(i+" "+month+" "+ position+" ");
                    break;  }
                else if((month==4)|| (month==6)||(month==8)||(month==11)) 
                    position+=2;
                else if(month==2){
                    if((i%400==0)||((i%100!=0)&&(i%4==0))) 
                    position+=1;                
                    else  
                    position+=0; }
                else 
                    position+=3;

                if(position>7) position%=7;

                counter[position-1]++;

                System.out.println(i+" "+month+" "+ position+" ");
            }

            }

        for(int x : counter){
              System.out.print(x+" ");

                    }}

我真的很难过,因为我的逻辑给出了错误的答案。我所做的是花费额外的天数,即31天的3天,30天的2天等,并将其添加到该位置。但它给出了错误的答案。

我的逻辑出了什么问题。

我对陷入这个简单的问题感到非常失望。非常感谢所有帮助。

谢谢!

2 个答案:

答案 0 :(得分:3)

疑难杂症!

for (int i = 1900; i <= N; i++) {
        for (int month = 1; month <= 12; month++) {
            if ((i == 1900) && (month == 1)) {
                counter[position - 1]++;
                position = 31%7 + 1;
            }

有两个错误,首先应该有9个而不是8个。我们遵循的一般逻辑是我们知道1900年第一个第13天的那一天。一旦你进入1900年1月的代码,你需要做两件事。首先,星期六的增量计数,然后从1月开始31天,你循环找到第二天的第13天,即你从1900年1月13日到1900年2月13日在同一段代码中移动,这是通过增加31天完成的这是2月13日至1月13日之间的天数。要将其转换为一天,您需要执行31%7(在您的情况下为+1,因为您的编号从1开始)。所以在月份= 1月的循环中,你也会增加2月份。

对于month = Feb,您循环查找March的日期,并在for循环关闭时递增。类似地,在循环月份= 11月,您循环查找Decemeber的日期,然后如果年份是最后一年则中断,以便不会溢出到下一年。如果年份没有最终你进入

 if ((month == 4) || (month == 6) || (month == 9)
                || (month == 11))

并按照惯常做生意并在12月份增加而不会中断。对于月份= 12月,您将增加次年1月13日的日期计数,从而允许我们隔离1900年1月的特殊情况,因为任何其他年份的1月将跳过所有if语句并且

position += 3; 

没有任何问题。 特例:

if ((i == 1900) && (month == 1)) {
            counter[position - 1]++;
            position = 31%7 + 1;
        }

您的完整代码。

public static void main(String[] args) throws IOException {
    // Use BufferedReader rather than RandomAccessFile; it's much faster
    BufferedReader f = new BufferedReader(new FileReader(
            "/home/shaleen/USACO/friday/friday.in"));
    // input file name goes above

    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
            "/home/shaleen/USACO/friday/friday.out")));
    // Use StringTokenizer vs. readLine/split -- lots faster
    // StringTokenizer st = new StringTokenizer(f.readLine());
    // Get line, break into tokens.

    int n1 = Integer.parseInt(f.readLine());
    int[] counter = new int[7];

    int N = 1900 + n1 - 1;
    int position = 1; // first 13th is a Saturday

    for (int i = 1900; i <= N; i++) {
        for (int month = 1; month <= 12; month++) {
            if ((i == 1900) && (month == 1)) {
                counter[position - 1]++;
                position = 31%7 + 1;
            }
            else if ((i == N) && (month == 11)) {
                position += 2;
                position %= 7;
                counter[position - 1]++;
                System.out.println(i + " " + month + " " + position + " ");
                break;
            } else if ((month == 4) || (month == 6) || (month == 9)
                    || (month == 11))
                position += 2;
            else if (month == 2) {
                if ((i % 400 == 0) || ((i % 100 != 0) && (i % 4 == 0)))
                    position += 1;
                else
                    position += 0;
            } else
                position += 3;

            if (position > 7)
                position %= 7;

            counter[position - 1]++;

            System.out.println(i + " " + month + " " + position + " ");
        }

    }

    for (int x : counter) {
        System.out.print(x + " ");

    }
}
}

答案 1 :(得分:0)

正如评论中所指出的,你正在迭代11个月。你发帖后我试过这个问题,知道我是否遗漏了什么。除此之外它看起来还不错。如果12个月解决问题,请告诉我们。否则,我会尝试更多地查看您的代码。

我的推荐代码的SPOILER ALERT。

 HashMap<Integer, Integer> daysInAMonth = new HashMap<Integer, Integer>();
    daysInAMonth.put(0, 31);
    daysInAMonth.put(2, 31);
    daysInAMonth.put(3, 30);
    daysInAMonth.put(4, 31);
    daysInAMonth.put(5, 30);
    daysInAMonth.put(6, 31);
    daysInAMonth.put(7, 31);
    daysInAMonth.put(8, 30);
    daysInAMonth.put(9, 31);
    daysInAMonth.put(10, 30);
    daysInAMonth.put(11, 31);


    HashMap<Integer, Integer> dayFrequency = new HashMap<Integer, Integer>(7);
    //sat - 0
    //      sun -1
//      mon -2
//      tue -3
//      wed -4
//      thu -5 
//      fri -6

    dayFrequency.put(0, 0);
    dayFrequency.put(1, 0);
    dayFrequency.put(2, 0);
    dayFrequency.put(3, 0);
    dayFrequency.put(4, 0);
    dayFrequency.put(5, 0);
    dayFrequency.put(6, 0);



    int N = Integer.parseInt(st.nextToken());
    if (N==0) {
        out.println("0 0 0 0 0 0 0");
        System.exit(0);
    }
    System.out.println(N);
    int firstFriday = 0;
    int lastFriday = 0;
    for(int i=0 ;i<N; i++) {
        int year = 1900+i;

        for(int month=0; month<12;month++){

            if(month ==0 && year ==1900) {
                int prevCount  = dayFrequency.get(0);
                dayFrequency.put(0, prevCount + 1);
                lastFriday = 0;
            }


            int noOfdays;
            if(month != 1)
                noOfdays = daysInAMonth.get(month);
                else
                    noOfdays = daysInFebruary(year);

            int wrapDays = (noOfdays-13) + 13;

            lastFriday = (lastFriday + wrapDays)%7;

            dayFrequency.put(lastFriday, dayFrequency.get(lastFriday) + 1);

        }           
    }

    dayFrequency.put(lastFriday, dayFrequency.get(lastFriday) - 1);     


    for(int i=0;i<7;i++) {
        out.print(dayFrequency.get(i));
        if( i != 6)
            out.print(" ");
    }
    out.println("");
    out.close();
    System.exit(0);  
}
public static int daysInFebruary(int year) {
    if(year % 400 == 0)
        return 29;
    if(year % 4 == 0 && year % 100 != 0)
        return 29;
    return 28;      
}   
}
相关问题