1. 程式人生 > >藍橋杯 ALGO-105 黑色星期五(日期)

藍橋杯 ALGO-105 黑色星期五(日期)

【思路】:和CCF的一個題目差不多,先算天數,根據天數算星期。

【AC程式碼】:

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;

int isLeapYear(int year)
{
	if (((0 == year%4) && (0 != year%100)) || (0 == year%400))
		return 1;
	return 0;
}

int getDayOfEarlyYear(int year)
{
	int i = 0;
	int sum_day = 0;
	for (i = 1998; i < year; i++)
	{
		if (isLeapYear(i))
			sum_day += 366;
		else
			sum_day += 365;
	}
	return sum_day;
}

int getDayOfMonth(int month, int year)
{
	int i = 0;
	int sum_day = 0;
	for (i = 1; i < month; i++)
	{
		switch(i)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				sum_day += 31;
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				sum_day += 30;
				break;
			case 2:
				if (isLeapYear(year))
					sum_day += 29;
				else
					sum_day += 28;
				break;
		}
	}
	return sum_day;
} 

int main()
{
	//freopen("in.txt", "r", stdin);
	int y = 0, sum_day = 0, cnt = 0, i = 0;
	cin >> y;
	
	//加上前幾年的天數
	sum_day += getDayOfEarlyYear(y);
	//判斷每個月的13號 
	for (i = 1; i <= 12; i++)
	{
		int temp = sum_day + getDayOfMonth(i, y) + 13;
		int w = ((temp%7)+4)%7-1;
		if (5 == w)
			cnt++;
	}
	
	//output
	cout << cnt; 
	return 0;
}