黑色星期五

code

#include<iostream>
using namespace std;
int main() {
	int is_leap_year(int);
	int year_days(int);//one year have days
	int month_days(int);
	int week(int, int, int);
	int y, m, c = 0;
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> y;
	for (m = 1; m <= 12; ++m) {
		if (5 == week(y, m, 13)) {
			++c;
		}
	}
	cout << c;
	return 0;
}
int is_leap_year(int year) {
	return (0 == year % 4 && 0 != year % 100 || 0 == year % 400) ? 1 : 0;
}
int year_days(int year) {
	return is_leap_year(year) ? 366 : 365;
}
int month_days(int year, int month) {
	int days;
	if (1 == month || 3 == month || 5 == month || 7 == month || 8 == month || 10 == month || 12 == month) {
		days = 31;
	} else if (2 == month) {
		if (is_leap_year(year)) {
			days = 29;
		} else {
			days = 28;
		}
	} else {
		days = 30;
	}
	return days;
}
int week(int year, int month, int day) {
	day += 3;


	while (--month) {
		day += month_days(year, month);
	}
	while (1998 != year--) {
		day += year_days(year);
	}

	return day%7;
}

ref

Q

试题 算法训练 黑色星期五
     
资源限制
时间限制:1.0s   内存限制:512.0MB

问题描述
  有些西方人比较迷信,如果某个月的13号正好是星期五,他们就会觉得不太吉利,用古人的说法,就是“诸事不宜”。请你编写一个程序,统计出在某个特定的年份中,出现了多少次既是13号又是星期五的情形,以帮助你的迷信朋友解决难题。
  说明:(1)一年有365天,闰年有366天,所谓闰年,即能被4整除且不能被100整除的年份,或是既能被100整除也能被400整除的年份;(2)已知1998年1月1日是星期四,用户输入的年份肯定大于或等于1998年。
  输入格式:输入只有一行,即某个特定的年份(大于或等于1998年)。
  输出格式:输出只有一行,即在这一年中,出现了多少次既是13号又是星期五的情形。
输入输出样例
样例输入
1998
样例输出
3
posted @ 2022-02-24 10:52  ethon-wang  阅读(102)  评论(0编辑  收藏  举报