Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我的USACO周五第十三个解决方案有什么问题?_Java - Fatal编程技术网

Java 我的USACO周五第十三个解决方案有什么问题?

Java 我的USACO周五第十三个解决方案有什么问题?,java,Java,来自USACO培训页面,十三号星期五 问题是要计算从1990年1月1日到1990年12月31日,13号在一周中的每一天降落的频率+N-1,其中N是给定的。1990年1月1日是星期一,闰年也被考虑在内。程序应该在周六打印第13个数字,然后在周日打印第13个数字,以此类推到周五。 我似乎在我的代码中找不到问题,我已经试了几天了。编辑-不允许对程序使用日期函数 我的N=20的输出是37 34 35 36 35 应该是363334333534 import java.io.*; class friday

来自USACO培训页面,十三号星期五 问题是要计算从1990年1月1日到1990年12月31日,13号在一周中的每一天降落的频率+N-1,其中N是给定的。1990年1月1日是星期一,闰年也被考虑在内。程序应该在周六打印第13个数字,然后在周日打印第13个数字,以此类推到周五。 我似乎在我的代码中找不到问题,我已经试了几天了。编辑-不允许对程序使用日期函数 我的N=20的输出是37 34 35 36 35

应该是363334333534

import java.io.*;
class friday {

public static void main(String[] args) throws IOException {
    BufferedReader in = new BufferedReader(new FileReader("friday.in"));
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));

    int N = Integer.parseInt(in.readLine());
    int[] monthDays = {0,31,28,31,30,31,30,31,31,30,31,30,31};

    int firstdayNo=2;
    int THIRdayNo;
    int[] counter = new int[8];
    Arrays.fill(counter, 1); 
    for (int j=1900; j<1900+N;j++) {
        if (j%4==0 && j%100 !=0 || j%400==0) { 
            monthDays[2]=29;
        }

        else {
            monthDays[2]=28;
        }



        for (int i=1; i<13; i++) {
            if (firstdayNo==2) {
                THIRdayNo=7;

            }
            else {
                THIRdayNo=(firstdayNo+12)%7; 
            }
            counter[THIRdayNo]++;
            firstdayNo= (monthDays[i]%7) +firstdayNo; 

            if (firstdayNo>7) {firstdayNo=firstdayNo-7;}
            // to here, and you are done
            }

    }
    out.println(+counter[7]+" "+ counter[1]+" "+counter[2]+" "+ counter[3]+ " "+counter[4]+" "+counter[5]+" "+counter[6]);
    in.close();
    out.close();
    System.exit(0);
}   

}
import java.io.*;
星期五上课{
公共静态void main(字符串[]args)引发IOException{
BufferedReader-in=new-BufferedReader(new-FileReader(“friday.in”);
PrintWriter out=新的PrintWriter(新的BufferedWriter(新的FileWriter(“friday.out”));
int N=Integer.parseInt(in.readLine());
int[]月日={0,31,28,31,30,31,31,31,30,31};
int firstdayNo=2;
int THIRdayNo;
int[]计数器=新的int[8];
数组。填充(计数器,1);

对于(int j=1900;j
java.util.Calendar
,以
GregoriaCalendar
作为标准民事日期,已经可以处理从一个月到另一个月的事务(只需使用
Calendar.add(Calendar.month,1)
),还可以跟踪闰年,并知道如何处理周中的一天。因此:

  static public void main(String[] arg) {
    int N=20;

    TreeMap<Integer, Integer> dayOfWeekOccurs=new TreeMap<>();
    // DAY_OF_WEEK is 1-based
    for(int dayOfWeek=1; dayOfWeek<=7; dayOfWeek++) {
      dayOfWeekOccurs.put(dayOfWeek, 0);
    }

    // month is 0-based, DAY_OF_MONTH is 1-based, YEAR is 0-based.
    // Consistency be damn'd.
    GregorianCalendar start=new GregorianCalendar(1990, 0, 13);
    GregorianCalendar end=new GregorianCalendar(1990+N-1, 11, 31);

    while(start.compareTo(end)<0) {
      start.add(Calendar.MONTH, 1);
      int dayOfWeek=start.get(Calendar.DAY_OF_WEEK);
      int oldOccurs=dayOfWeekOccurs.get(dayOfWeek);
      dayOfWeekOccurs.put(dayOfWeek, ++oldOccurs);
    }

    System.out.println("13 on Sun: "+dayOfWeekOccurs.get(Calendar.SUNDAY)); 
    System.out.println("13 on Mon: "+dayOfWeekOccurs.get(Calendar.MONDAY)); 
    System.out.println("13 on Tue: "+dayOfWeekOccurs.get(Calendar.TUESDAY)); 
    System.out.println("13 on Wed: "+dayOfWeekOccurs.get(Calendar.WEDNESDAY)); 
    System.out.println("13 on Thu: "+dayOfWeekOccurs.get(Calendar.THURSDAY)); 
    System.out.println("13 on Fri: "+dayOfWeekOccurs.get(Calendar.FRIDAY)); 
    System.out.println("13 on Sat: "+dayOfWeekOccurs.get(Calendar.SATURDAY)); 
  }
static public void main(字符串[]arg){
int N=20;
TreeMap dayOfWeekOccurs=new TreeMap();
//每周的第天是以1为基础的

对于(int dayOfWeek=1;dayOfWeek“应该在周六打印星期五的数字”这里有点问题:我无法想象星期五怎么会发生在星期六。对不起,我刚刚修复了它。我的解释有点改变了-你也不能使用日期函数来解决这个问题:(@KabbageSenthilkumar太糟糕了(无论谁禁止你这么做都有一些虐待狂倾向)。那么我想你仍然可以使用这种技术来诊断代码的问题:将代码输出到13号的星期几,然后使用格雷戈里代码来发现它们何时不同步并诊断原因。(顺便说一句,一定要尝试使用基于0的数组,使用基于1的数组时,代码看起来很笨拙)。