USACO 1.1 – Friday the Thirteenth | Jack Neus
Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.
http://jackneus.com/programming-archives/friday-the-thirteenth/
Read full article from USACO 1.1 – Friday the Thirteenth | Jack Neus
Is Friday the 13th really an unusual event?
That is, does the 13th of the month land on a Friday less often than on any other day of the week? To answer this question, write a program that will compute the frequency that the 13th of each month lands on Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, and Saturday over a given period of N years. The time period to test will be from January 1, 1900 to December 31, 1900+N-1 for a given number of years, N. N is positive and will not exceed 400.
http://jackneus.com/programming-archives/friday-the-thirteenth/
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int fdays[7] = {0, 0, 0, 0, 0, 0, 0};
bool leap(int y){
return (y % 100 == 0 && y % 400 == 0 || y % 100 != 0 && y % 4 == 0);
}
int main(){
int N;
fin >> N;
int year = 1900;
int day = 2;
for(int year = 1900; year < 1900 + N; year++){
for(int month = 0; month < 12; month++){
int daysInMonth = days[month];
if(month == 1 && leap(year)) daysInMonth++;
for(int d = 1; d <= daysInMonth; d++){
if(d == 13) fdays[day]++;
day = (day + 1) % 7;
}
}
}
for(int i = 0; i < 7; i++){
fout << (i ? " " : "") << fdays[i];
}
fout << endl;
}
http://code.antonio081014.com/2012/09/usaco-friday-thirteenth.htmlpublic void solve() throws Exception { BufferedReader br = new BufferedReader(new FileReader("friday.in")); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("friday.out"))); counts = new int[7]; int N = Integer.parseInt(br.readLine()); int days = 0; for (int y = 1900; y < 1900 + N; y++) { for (int m = 1; m <= 12; m++) { counts[(days + 13) % 7]++; days += getDays(y, m); days %= 7; } } String ret = ""; for (int i = 0; i < 7; i++) ret += " " + counts[(i + 6) % 7]; out.write(ret.substring(1) + "\n"); out.close(); } public int getDays(int Y, int M) { if (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12) { return 31; } else if (M == 4 || M == 6 || M == 9 || M == 11) { return 30; } else { if ((Y % 100 == 0 && Y % 400 == 0) || (Y % 100 != 0 && Y % 4 == 0)) { return 29; } else { return 28; } } }https://gist.github.com/stphung/914910
Read full article from USACO 1.1 – Friday the Thirteenth | Jack Neus