2016-10-08 7 views
-3

USACO Training Page、金曜日13日 問題は、1990年1月1日から1990年12月31日+ N-1(Nが与えられている)の13日が毎週上陸する頻度を計算すると言います。 1990年1月1日は月曜日であり、うるう年が考慮されます。プログラムは土曜日の13日の数字を、金曜日までの日曜日の13日の数字を印刷する必要があります。 私のコードでこの問題を見つけることができないようで、数日前から試してみました。 EDITは - Nのための私の出力は= 20がそれをすることになっています37 34 35 34 36 36 35私のUSACO Friday the 13th solutionの問題は何ですか?

ある あなたはプログラムの日付関数を使用することを許可されていない36 33 34 33 35 35 34

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); 
} 

} 
+0

何かがここで間違っている「金曜日の土曜日に13thsを番号を印刷する必要があります」:私は金曜日の缶は、土曜日に起こってくるか想像することはできません。 –

+0

申し訳ありませんが、私はそれを修正しました – MathmagicalWasp

答えて

0

GregorianCalendarを標準の市民日付として使用しており、月単位で処理しています(ちょうどCalendar.add(Calendar.MONTH, 1)を使用)。また、うるう年を追跡し、DAY_OF_WEEKの処理方法を知っています。だから、:

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)); 
    } 
+0

私の説明を少し変更しました - また、問題の日付関数を使用することはできません:( – MathmagicalWasp

+0

@ KabbageSenthilkumarあまりにも悪い(誰かがそれをすることを禁じるいくつかのサディスティックな傾向があります)。あなたのコードで13日目のコード出力を行い、グレゴリオ暦のコードを使用して、同期が外れてその理由を診断することができます(BTW、0-コードは1ベースのものでは扱いにくい)。 –

関連する問題