编程语言
首页 > 编程语言> > 《算法笔记》3.4小节——入门模拟->日期处理 问题 B: Day of Week

《算法笔记》3.4小节——入门模拟->日期处理 问题 B: Day of Week

作者:互联网

题目描述

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.

样例输入 Cop

y
21 December 2012
5 January 2013

样例输出 Copy

Friday
Saturday

代码

初出茅庐,请多指教!

#include<stdio.h>         //today is 18 February 2021,it is Thursday.
#include<string.h>
char January[20]="January";
char February[20]="February";
char March[20]="March";
char April[20]="April";
char May[20]="May";
char June[20]="June";
char July[20]="July";
char August[20]="August";
char September[20]="September";
char October[20]="October";
char November[20]="November";
char December[20]="December";
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 isleap(int year){
	if((year%4==0&&year%100!=0)||year%400==0) return 1;
	else return 0;
}

int main(){
	int d,m,y;
	char M[20];
	int dayofweek[1000]={0};
	int num=0;
	while(scanf("%d%s%d",&d,M,&y)!=EOF){
		if(y<1000||y>3000){
			printf("year error!\n");
			return 0;
		}
		
		if(strcmp(M,January)==0)  m=1;
		else if(strcmp(M,February)==0)  m=2;
		else if(strcmp(M,March)==0)  m=3;
		else if(strcmp(M,April)==0)  m=4;
		else if(strcmp(M,May)==0)  m=5;
		else if(strcmp(M,June)==0)  m=6;
		else if(strcmp(M,July)==0)  m=7;
		else if(strcmp(M,August)==0)  m=8;
		else if(strcmp(M,September)==0)  m=9;
		else if(strcmp(M,October)==0)  m=10;
		else if(strcmp(M,November)==0)  m=11;
		else if(strcmp(M,December)==0)  m=12;
		else {
			printf("month error!\n");
			return 0;
		}
		
		if(d<1||d>month[m][isleap(y)]){
			printf("day error!\n");
			return 0;
		}
		
		//printf("%d %d %d\n",d,m,y);
		int x=0;
		if(y*10000+m*100+d<=20210218){
			while(y<2021||m<2||d<18){
				d++;
				if(d==month[m][isleap(y)]+1){
					m++;
					d=1;
				}
				if(m==13){
					y++;
					m=1;
				}			
				x++;
			}
			//printf("x=%d\n",x);
			if(x%7==0) dayofweek[num]=4;        //2/11  2/18
			else if(x%7==6) dayofweek[num]=5;   //2/12 5
			else if(x%7==5) dayofweek[num]=6;   //2/13 6
			else if(x%7==4) dayofweek[num]=7;   //2/14 7 
			else if(x%7==3) dayofweek[num]=1;   //2/15 1
			else if(x%7==2) dayofweek[num]=2;   //2/16 2
			else if(x%7==1) dayofweek[num]=3;     //2/17 3
		}
		else if(y*10000+m*100+d>20210218){
			while(y>2021||m>2||d>18){
				d--;
				if(d==0){
					m--;
					if(m==0){
						y--;
						m=12;
					}		
					d=month[m][isleap(y)];
				}					
				x++;
			}
			if(x%7==0) dayofweek[num]=4;        //2/25 4
			else if(x%7==6) dayofweek[num]=3;   //2/24 3
			else if(x%7==5) dayofweek[num]=2;   //2/23 2
			else if(x%7==4) dayofweek[num]=1;   //2/22 1 
			else if(x%7==3) dayofweek[num]=7;   //2/21 7
			else if(x%7==2) dayofweek[num]=6;   //2/20 6
			else if(x%7==1) dayofweek[num]=5;     //2/19 5
			
		}
				
		num++;
	}
	
	int i;
	for(i=0;i<num;i++){
		if(dayofweek[i]==4) printf("Thursday\n");
		else if(dayofweek[i]==5) printf("Friday\n");
		else if(dayofweek[i]==6) printf("Saturday\n");
		else if(dayofweek[i]==7) printf("Sunday\n");
		else if(dayofweek[i]==1) printf("Monday\n");
		else if(dayofweek[i]==2) printf("Tuesday\n");
		else if(dayofweek[i]==3) printf("Wednesday\n");
	}
	
	return 0;
}


标签:Week,dayofweek,20,31,else,3.4,num,x%,Day
来源: https://blog.csdn.net/weixin_43195640/article/details/113849380