Usaco Training网站
关注我持续更新usaco training题目
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 Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, and Friday 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.
Note that the start year is NINETEEN HUNDRED, not NINETEEN NINETY.
There are few facts you need to know before you can solve this problem:
- January 1, 1900 was on a Monday.
- Thirty days has September, April, June, and November, all the rest have 31 except for February which has 28 except in leap years when it has 29.
- Every year evenly divisible by 4 is a leap year (1992 = 4*498 so 1992 will be a leap year, but the year 1990 is not a leap year)
- The rule above does not hold for century years. Century years divisible by 400 are leap years, all others are not. Thus, the century years 1700, 1800, 1900 and 2100 are not leap years, but 2000 is a leap year.
Do not use any built-in date functions in your computer language.
Don't just precompute the answers, either, please.
PROGRAM NAME: friday
INPUT FORMAT
One line with the integer N.SAMPLE INPUT (file friday.in)
20OUTPUT FORMAT
Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.SAMPLE OUTPUT (file friday.out)
36 33 34 33 35 35 34
跟之前一样,我手动翻译一下,大意:
求出1900年后的n年(包括1900),每年的每个月的13号各是周几
输入一个n
输出13号在周六、周日、周一、周二、周三、周四、周五的次数
题目 起来很简单对吧?
但是他的这个代码量我真是醉了
。。。还有这个行数明显就是在侮辱人
😅😅😅
言归正传,这题需要考虑的因素真的不少
闰年、月份天数
闰年写个闰年判断程序即可
至于月份嘛…………
直接枚举!(代码量就在这上面多的)
然后代码也就可以轻易写出
/*
ID:
TASK:friday
LANG:C++
*/
# include <iostream>
# include <cstdio>
using namespace std;
# define int long long
int n;
bool isrun(int year){
if ((year%4==0&&year%100!=0)||(year%400==0)){
return true;
}else{
return false;
}
}
signed main(){
freopen("friday.in","r",stdin);
freopen("friday.out","w",stdout);
scanf("%lld",&n);
int ans[10]={};
int day=1;
for (int i=1900;i<1900+n;i++){
if (isrun(i)){//闰年
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<29;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
}else{
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<28;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<30;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
for (int j=0;j<31;j++){
if (j==13){
ans[day-1]++;
}
day++;
if (day>7){
day=1;
}
}
}
}
printf("%lld %lld %lld %lld %lld %lld %lld\n",ans[6],ans[0],ans[1],ans[2],ans[3],ans[4],ans[5]);
fclose(stdin);
fclose(stdout);
return 0;
}