根据输入具体年月日,输出该日子为星期几(C/C++)

输出格式

输出星期几。
MondayTuesdayWednesdayThursdayFridaySaturdaySunday表示星期。

样例输入1

1 1 1

样例输出1

Monday

样例输入2

2016 11 29

样例输出2

Tuesday

具体的代码实现

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

int whatday(int y, int m, int d)
{
	// 返回正确的星期.用0-6表示星期1-7
	int ans = 0;
	for (int i = 1; i < y; i++)
	{
		if (((i % 100) != 0 && i % 4 == 0) || i % 400 == 0)
		{
			ans += 366 % 7;
			ans %= 7;
		}
		else
		{
			ans += 365 % 7;
			ans %= 7;
		}
	}
	for (int i = 1; i < m; i++)
	{
		if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12)
		{
			ans += 31 % 7;
			ans %= 7;
		}
		else if (i == 4 || i == 6 || i == 9 || i == 11)
		{
			ans += 30 % 7;
			ans %= 7;
		}
		else if ((y % 100 != 0 && y % 4 == 0) || i % 400 == 0)
		{
			ans += 29 %7;
			ans %= 7;
		}
		else
		{
			ans += 28 %7;
			ans %= 7;
		}
	}
	ans += (d - 1) % 7;
	ans %= 7;
	return ans;
}
string weekday[7] =
{
	"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"
};
int main()
{
	int y, m, d;
	cin >> y >> m >> d;
	cout << weekday[whatday(y, m, d)] << endl;
	return 0;
}
上一篇:4.3 java枚举类型


下一篇:并查集总结<2021.August.2.Monday><第四篇>