ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2021-02-18 17:00:47  阅读:254  来源: 互联网

标签:Week dayofweek 20 31 else 3.4 num x% Day


题目描述

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有