Pages

Friday, December 14, 2007

USACO: Friday the Thirteenth

/*
ID: wsoethe1
LANG: JAVA
TASK: friday
*/

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.GregorianCalendar;

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

try {
int anos = Integer.parseInt(fileIn.readLine());
int[] trezes = { 0, 0, 0, 0, 0, 0, 0 };
int dow = 0;
int d = 13;
for (int i = 0; i < anos; i++) {
for (int j = 1; j <= 12; j++) {
if (i == 200 && j >= 3)
d = 12;
if (i >= 201)
d = 12;
if (i == 300 && j >= 3)
d = 11;
if (i >= 301)
d = 11;
dow = (int) ((calcJD(1900 + i, j, d) + 1.5) % 7);
trezes[dow] = trezes[dow] + 1;
}
}
fileOut.write(trezes[6] + " " + trezes[0] + " " + trezes[1]
+ " " + trezes[2] + " " + trezes[3] + " " + trezes[4]
+ " " + trezes[5] + "\n");
} finally {
fileIn.close();
fileOut.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static double calcJD(int y, int m, int d) {
double extra = 100.0 * y + m - 190002.5;
double jd = 367.0 * y;
jd -= Math.floor(7.0 * (y + Math.floor((m + 9.0) / 12.0)) / 4.0);
jd += Math.floor(275.0 * m / 9.0);
jd += d;
jd += (0 + (0 + 0 / 60.0) / 60.) / 24.0;
jd += 1721013.5;
jd -= 0.5 * extra / Math.abs(extra);
jd += 0.5;
return jd;
}
}

No comments :