(USACO) Friday the Thirteenth - Solution
import java.io.*;
import java.util.*;
class friday {
public static int[] weekDays = new int[8];
public static int[] monthLength = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static int month(int weekDay, int numDays) {
for (int i = 1; i <= numDays; i++) {
if (i == 13) {
weekDays[weekDay] += 1;
}
weekDay++;
if (weekDay == 8) {
weekDay = 1;
}
}
return weekDay;
}
public static boolean isLeapYear(int year) {
boolean leapYear = false;
if (year%100 == 0) { // century
if (year%400 == 0) {
leapYear = true;
}
}
else {
if (year%4 == 0) {
leapYear = true;
}
}
return leapYear;
}
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("friday.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
int entry = Integer.parseInt(f.readLine());
int firstYear = 1900;
int lastYear = firstYear+entry;
int wday = 3; // {"Sat = 1", "Sun = 2", "Mon = 3", "Tue = 4", "Wed = 5", "Thu = 6", "Fri = 7"};
for (int i = firstYear; i < lastYear; i++) {
for (int j = 1; j <= 12; j++) {
int monthLen = monthLength[j];
if (j == 2 && isLeapYear(i)) {
monthLen += 1;
}
wday = month(wday, monthLen);
}
}
for (int i = 1; i < 7; i++) {
out.print(weekDays[i] + " ");
}
out.println(weekDays[7]);
out.close();
}
}
import java.util.*;
class friday {
public static int[] weekDays = new int[8];
public static int[] monthLength = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
public static int month(int weekDay, int numDays) {
for (int i = 1; i <= numDays; i++) {
if (i == 13) {
weekDays[weekDay] += 1;
}
weekDay++;
if (weekDay == 8) {
weekDay = 1;
}
}
return weekDay;
}
public static boolean isLeapYear(int year) {
boolean leapYear = false;
if (year%100 == 0) { // century
if (year%400 == 0) {
leapYear = true;
}
}
else {
if (year%4 == 0) {
leapYear = true;
}
}
return leapYear;
}
public static void main (String [] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("friday.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out")));
int entry = Integer.parseInt(f.readLine());
int firstYear = 1900;
int lastYear = firstYear+entry;
int wday = 3; // {"Sat = 1", "Sun = 2", "Mon = 3", "Tue = 4", "Wed = 5", "Thu = 6", "Fri = 7"};
for (int i = firstYear; i < lastYear; i++) {
for (int j = 1; j <= 12; j++) {
int monthLen = monthLength[j];
if (j == 2 && isLeapYear(i)) {
monthLen += 1;
}
wday = month(wday, monthLen);
}
}
for (int i = 1; i < 7; i++) {
out.print(weekDays[i] + " ");
}
out.println(weekDays[7]);
out.close();
}
}
Comments
Post a Comment