题解 | #Day of Week#

Day of Week

https://www.nowcoder.com/practice/a3417270d1c0421587a60b93cdacbca0

#include <iostream>
using namespace std;
int isleap(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
	{
		return 1;
	}
	else return 0;
}//判断是否为闰年、

int main()
{
string englishday[7] = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"};
string englishmon[13] = { " ","January","February","March","April","May","June","July","August","September","October","November","December"};
int year, day,i;
int mon[13] = { 0,31,0,31,30,31,30,31,31,30,31,30,31 };
	string a;
	
	while ((cin>>day>>a>>year))
	{ int pastyear = year;
	int nowyear = 2024;
		int temp1[2] = { 365,366 };
		int i = 1;
		int sum = 0;
		while (englishmon[i] != a)
		{
			i++;//将英文月份转化为中文月份
		}		
		while (pastyear!= nowyear)
		{
			if (pastyear < nowyear)
			{
				sum += temp1[isleap(pastyear)];
				pastyear++;
			}
			else if(pastyear>nowyear)
			{
				sum += temp1[isleap(nowyear)];
				nowyear++;
			}
	    }//将年份对齐
		if (isleap(year))
		{
			mon[2] = 29;
		}
		else 
		{
			mon[2] = 28;

		}
		for (int j = 1; j < i; j++)
		{
			sum += mon[j];
		}//将月份对齐
		sum =(sum+day-1)%7;
		cout <<englishday[sum];
		printf("\n");
	}
	return 0;
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务