usaco:星期五十三我的逻辑有什么问题? [英] usaco: friday the thirteen what's wrong with my logic?

查看:84
本文介绍了usaco:星期五十三我的逻辑有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The question asks to calculate the number of 13ths that fall on each day of the week. This is my code.

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天等,并将其添加到该位置。但它给出了错误的答案。

I'm really stumped because my logic gives the wrong answer. What I've done is to take the additional number of days, that is 3 for 31 day months, 2 for 30 day months etc and add it to the position. But it gives the wrong answer.

我的逻辑出了什么问题。

What's wrong with my logic.

我感觉很沮丧陷入这个简单的问题。非常感谢所有帮助。

I'm feeling really down at being stuck at this simple problem. All help is greatly appreciated.

谢谢!

推荐答案

Gotcha !

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天,你循环找到2月的第13天,即你从1900年1月13日到1900年2月13日在同一段代码中移动,这是通过增加31天完成的这是2月13日至1月13日之间的天数。要将其转换为一天,您需要执行31%7(在您的情况下+1,因为您的编号从1开始)。因此,在月份= 1月的循环中,您也会增加2月份。

There were two mistakes, first that 9 should be there instead of 8. The general logic we've followed is that we know the day of the first ever 13th in 1900. Once you are in the code for January 1900, you need to do two things. First, increment count for Saturday and then since Jan has 31 days, you loop over to find the day 13th falls on in February i.e you move from 13th January 1900 to 13th February 1900 in the same piece of code which is accomplished by adding 31 days which is the number of days between 13 Feb and 13 Jan. To translate this into a day you do the 31%7(+1 in your case as your numbering starts from 1). So in the loop for month = January, you increment for Feb as well.

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

For month = Feb you loop over to find day for March and increment when the for loop closes. Similarly in the loop month = Nov, you loop over to find the day for Decemeber and then break if the year is the final year so as not spill over into the next year. If the year isnt final you go into

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

并且做你通常的业务和12月的增量没有打破。对于月份= 12月,您将增加次年1月13日的日期计数,从而允许我们隔离1900年1月的特殊情况,因为任何其他年份的1月将跳过所有if语句并且

and do your usual business and increment for December without breaking. For month = December you increment the day count for 13 January of the following year thus allowing us to isolate our special case for January 1900 since January of any other year will skip all if statements and do

position += 3; 

没有任何问题。
特例:

without any problem. Special case :

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

    }
}
}

这篇关于usaco:星期五十三我的逻辑有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆