Skip to content

Instantly share code, notes, and snippets.

@stphung
Created April 12, 2011 04:15
Show Gist options
  • Save stphung/914910 to your computer and use it in GitHub Desktop.
Save stphung/914910 to your computer and use it in GitHub Desktop.
My solution to "Friday the Thirteenth" from USACO
/*
ID: stphung1
LANG: JAVA
TASK: friday
*/
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
class friday {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("friday.in"));
PrintWriter pw = new PrintWriter(new File("friday.out"));
int years = sc.nextInt();
int freq[] = new int[7];
Arrays.fill(freq, 0);
int calendarDay = 0;
int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int[] leapDays = new int[] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int end = 1900 + years;
for (int year = 1900; year < end; year++) {
for (int month = 0; month < 12; month++) {
int day = (calendarDay + 12) % 7;
freq[day]++;
calendarDay += isLeap(year) ? leapDays[month] : days[month];
}
}
pw.print(freq[5]);
pw.print(' ');
pw.print(freq[6]);
pw.print(' ');
pw.print(freq[0]);
pw.print(' ');
pw.print(freq[1]);
pw.print(' ');
pw.print(freq[2]);
pw.print(' ');
pw.print(freq[3]);
pw.print(' ');
pw.println(freq[4]);
pw.close();
}
public static boolean isLeap(int year) {
if (year % 100 == 0) {
return year % 400 == 0;
} else {
return year % 4 == 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment