知道了!
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月的代码,就需要两件事情。首先,计算周六的增量计数,然后由于一月有31天,因此您可以循环查找2月的第13天,即您以相同的代码从1900年1月13日移至1900年2月13日,方法是增加31天这是2月13日至1月13日之间的天数。要将其转换为一天,请执行31%7(在您的情况下为+1,因为您的编号从1开始)。因此,在月=一月的循环中,二月也将增加。
对于month = Feb,您循环查找三月的日期,并在for循环关闭时增加。同样,在循环月份=
11月中,您循环查找以查找Decemeber的日期,然后在年份为最后一年的情况下中断,以免溢出到下一年。如果年份不是最后的年份,则进入
if ((month == 4) || (month == 6) || (month == 9) || (month == 11))
并按惯例进行业务并在12月份继续增加而不会中断。对于month =
December,您增加了第二年1月13日的天数,因此使我们能够隔离1900年1月的特殊情况,因为任何其他年份的1月都会跳过所有if语句并执行
position += 3;
没有任何问题。特殊情况 :
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 + " "); }}}


