From USACO Training Page,Friday 13th 問題描述爲計算第13次將在1990年1月1日至1990年12月31日的每一天的每一天着陸的頻率,其中N是給定的。 1990年1月1日是星期一,閏年被考慮在內。該計劃應打印週六13日的數字,週日13日的數字等,直至週五。 我似乎無法在我的代碼中找到問題,而且我已經嘗試了幾天。編輯 - 你不允許使用日期函數爲程序 我給N個輸出= 20 37 34 35 34 36 36 35我的USACO星期五第十三個解決方案有什麼問題?
它應該是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);
}
}
「應該打印星期六13號週五的號碼」這裏有什麼不對勁:我無法想象週六怎麼會發生星期五。 –
對不起,我不喜歡修復它 – MathmagicalWasp