2017-05-20
题目大意:
给定数字N (1<=N<=400),计算1900年到1900+N-1年里,每个月的13号出现在周六、周日、周一、……周五的次数。并按顺序输出。
样例输入:
20
样例输出:
36 33 34 33 35 35 34
题解:
已知1900年1月1日是星期一,判断润年的方式是:
int isLeap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
接下来只要从1900年1月1号开始枚举到1900+N-1年的最后一天,把所有13号都记录下来放在result[]数组里即可。
代码:
/*
ID: zachery1
PROG: friday
LANG: C++
*/
#include <iostream>
#include <fstream>
#include <cstring>
#define cin fin
#define cout fout
using namespace std;
ifstream fin("friday.in");
ofstream fout("friday.out");
int dayOfMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int result[7];
int N;
int main() {
memset(result, 0, sizeof(result));
cin >> N;
int weekday = 1;
for (int year = 1900; year <= 1900 + N - 1; year++) {
int isLeap = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) ? 1 : 0;
dayOfMonth[2] += isLeap;
for (int month = 1; month <= 12; month++) {
for (int day = 1; day <= dayOfMonth[month]; day++) {
if (day == 13) {
result[weekday] += 1;
}
weekday = (weekday + 1) % 7;
}
}
dayOfMonth[2] -= isLeap;
}
cout << result[6] << " ";
for (int i = 0; i < 5; i++) cout << result[i] << " ";
cout << result[5] << endl;
return 0;
}