问题 B: Day of Week

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34432960/article/details/87891277

题目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

输入

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

输出

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

样例输入

21 December 2012
5 January 2013

样例输出

Friday
Saturday
#include<stdio.h>
#include<string.h>
int month[13][2]={{0,0},{31,31},{28,29},{31,31},{30,30},{31,31},{30,30},{31,31},{31,31},{30,30},{31,31},{30,30},{31,31}};
int islyear(int a)
{
	if((a%4==0&&a%100!=0)||a%400==0)
	{
		return 1;
	}
	else return 0;
}
int sum(int y,int m,int d)
{
	int i,day=0;
	for(i=0;i<y;i++)
	{
		if(islyear(i))
		{
			day+=366;
		}else
		{
			day+=365;
		}
	}
	for(i=0;i<m;i++)
	{
		day+=month[i][islyear(y)];
	}
	for(i=1;i<d;i++)
	{
		day++;
	}
	return day;	
}
int findmonth(char *month)	//?
{
	//printf("input_month=%s\n",month);

	if(strcmp(month,"January")==0)return 1;
	if(strcmp(month,"January")==0)return 1;
	if(strcmp(month,"February")==0)return 2;
	if(strcmp(month,"March")==0)return 3;
	if(strcmp(month,"April")==0)return 4;
	if(strcmp(month,"May")==0) return 5;
	if(strcmp(month,"June")==0)return 6;
	if(strcmp(month,"July")==0)return 7;
	if(strcmp(month,"August")==0)return 8;
	if(strcmp(month,"September")==0)return 9;
	if(strcmp(month,"October")==0)return 10;
	if(strcmp(month,"November")==0)return 11;
	if(strcmp(month,"December")==0)return 12;
	
}
int main()
{
	int year,day;
	char input_month[20];
	while(scanf("%d%s%d",&day,input_month,&year)!=EOF)
	{
		int m=findmonth(input_month);
		//printf("the input is %d %d %d\n",day,m,year);
		int n=sum(year,m,day)-sum(2019,2,24);
		//printf("n1=%d\n",n);
		n=((n%7)+7)%7;
		//printf("n2=%d\n",n);
		switch(n)
		{
			case 1:
				printf("Monday\n");
				break;
			case 2:
				printf("Tuesday\n");
				break;
			case 3:
				printf("Wednesday\n");
				break;
			case 4:
				printf("Thursday\n");
				break;
			case 5:
				printf("Friday\n");
				break;
			case 6:
				printf("Saturday\n");
				break;
			case 0:
				printf("Sunday\n");
				break;
				
		}
	}
	//printf("%d\n",-16%7);
	return 0;
	
	
	
}

猜你喜欢

转载自blog.csdn.net/qq_34432960/article/details/87891277