我的第十三届usaco计划周五的逻辑出了什么问题?我已经尝试了一切

时间:2015-09-24 00:36:08

标签: java

我不明白为什么这段代码不起作用。我有几个月的天数,然后我从1900年到1900 + N + 1循环,以获得这些年的十三周五。然后我从几个月开始循环。我发现,无论是31天,28天还是30天的每一天,起始日都是星期一,下个月的开始日是星期一后的3天。所以我这样做了,我也发现每个第十三天之后只有13%在开始日后的7到1天所以我这样做了但我的代码仍然不起作用。每当我尝试打印它时,输出总是2,134,536或一些垃圾的数组值。有人可以帮我理解为什么它不对吗?

import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.util.*;

public class friday {

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

        // the input file 
        BufferedReader f = new BufferedReader(new FileReader("friday.txt"));


        // Use StringTokenizer vs. readLine/split -- lots faster
        int N = Integer.parseInt(f.readLine());


        int numdaysinmonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        int currentindexday = 0;

        int numberofThirtday[] = {0, 0 ,0 ,0 ,0 ,0, 0};

        //for loop from years
        for (int x = 1900; x < x + N; x++) {


             if(x%4 == 0 && x%100 != 0 || x%400==0 ){

             numdaysinmonth[1] = 29;

             }

             else{

             numdaysinmonth[1] = 28;
             }


            //the number of days to add to get from teh first starting day        

            int daystoadd = 0; 


            int startingday = 0;

            for (int y = 0; y < numdaysinmonth.length; y++) {

                if(numdaysinmonth[y] == 31){

                    daystoadd = 3;

                }

                else if(numdaysinmonth[y] == 30){
                    daystoadd = 2;
                }

                else if(numdaysinmonth[y] == 29){
                    daystoadd = 1;
                }

                else if(numdaysinmonth[y] == 28){
                    daystoadd = 0;
                }
    //each thirteenth day is the first day adding 5 to get the day 13%7 -1 
                currentindexday = startingday + 5;

                if(currentindexday>6){
                    currentindexday = currentindexday%7;
                }

                numberofThirtday[currentindexday]++;
                startingday  += daystoadd;
            }
        }

        for (int n = 0; n < numberofThirtday.length; n++) {
            System.out.println(numberofThirtday[n]);
        }
        out.close();                                   
        System.exit(0);                               
    }

}

0 个答案:

没有答案