《算法笔记》3.4小节——入门模拟->日期处理

目录

问题 A: 日期差值

问题 B: Day of Week

问题 C: 打印日期

问题 D: 日期类

问题 E: 日期累加


问题 A: 日期差值

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。

输入

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

输出

每组数据输出一行,即日期差值

样例输入

20130101
20130105

样例输出

5

题解

#include <cstdio>
#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}
};
bool isLeap(int year){//判断是否是闰年
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main()
{
	int time1, y1, m1, d1;
	int time2, y2, m2, d2;
	int temp;
	while(scanf("%d%d", &time1, &time2) != EOF){
		if(time1 > time2){
			temp = time1;
			time1 = time2;
			time2 = temp;
		}
		y1 = time1 / 10000, m1 = time1 % 10000 / 100, d1 = time1 % 100;
		y2 = time2 / 10000, m2 = time2 % 10000 / 100, d2 = time2 % 100;
		int count = 1;//记录日期差值
		while(y1 < y2 || m1 < m2 || d1 < d2){
			d1++;
			if(d1 == month[m1][isLeap(y1)] + 1){//满当月天数
				m1++;
				d1 = 1;//日期变为下个月的1号
			}
			if(m1 == 13){//满十二个月
				y1++;
				m1 = 1;//日期变为下一年的1月
			}
			count++;
		}
		printf("%d\n", count);
	}
	return 0;
}

问题 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.

样例输入

21 December 2012
5 January 2013

样例输出

Friday
Saturday

题解

#include <cstdio>
#include <string.h>
int day_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}
};
char month_year[13][10] = {//一年十二个月
	"0","January","February","March","April",
	"May","June","July","August","September",
	"October","November","December"
};
char week[7][10] = {//一周七天
	"Monday","Tuesday","Wednesday","Thursday",
	"Friday","Saturday","Sunday"
};
bool isLeap(int year){//判断是否是闰年
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main()
{
    int inday, inyear;
    char inmonth[10];
    int i, j;
    while(scanf("%d%s%d", &inday, inmonth, &inyear) != EOF){
        int sum = 0;//总天数 
        for(i = 1; i < inyear; i++){
            if(isLeap(i)){
				sum = sum + 366;
			}
            else{
				sum = sum + 365;
			}
        }
        for(i = 1; i <= 12; i++){
            if(strcmp(month_year[i], inmonth) == 0){
            	break;
			}
        }
        for(j = 1; j < i; j++){
        	sum = sum + day_month[j][isLeap(inyear)];
		}
        sum = sum + inday;
        if(sum % 7 == 0){
        	printf("Sunday\n");
		}
		else{
			printf("%s\n", week[sum % 7 - 1]);
		}
    }
	return 0;
}

问题 C: 打印日期

题目描述

给出年分m和一年中的第n天,算出第n天是几月几号。

输入

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出

可能有多组测试数据,对于每组数据,按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

样例输入

2013 60
2012 300
2011 350
2000 211

样例输出

2013-03-01
2012-10-26
2011-12-16
2000-07-29

题解

%d是普通的输出;%2d是将数字按宽度为2,采用右对齐方式输出,若数据位数不到2位,则左边补空格;%02d和%2d差不多,只不过左边补0。

#include <cstdio>
#include <string.h>
int day_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}
};
bool isLeap(int year){//判断是否是闰年
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main()
{
    int year, num;
    int i;
	int month = 1, day = 0;
    while(scanf("%d%d", &year, &num) != EOF){
        while(num > 0){
			day++;
			if(day == day_month[month][isLeap(year)] + 1){//满当月天数
				month++;
				day = 1;//日期变为下个月的1号
			}
			if(month == 13){//满十二个月
				year++;
				month=1;//日期变为下一年的1月
			}
			num--;
		}
		printf("%04d-%02d-%02d\n", year, month, day);
		month = 1, day = 0;//重新初始化 
    }
	return 0;
}

问题 D: 日期类

题目描述

编写一个日期类,要求按xxxx-xx-xx 的格式输出日期,实现加一天的操作。

输入

输入第一行表示测试用例的个数m,接下来m行每行有3个用空格隔开的整数,分别表示年月日。测试数据不会有闰年。

输出

输出m行。按xxxx-xx-xx的格式输出,表示输入日期的后一天的日期。

样例输入

2
1999 10 20
2001 1 31

样例输出

1999-10-21
2001-02-01

提示

注意个位数日期前面要有0。

题解

#include <cstdio>
#include <string.h>
int day_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}
};
bool isLeap(int year){//判断是否是闰年
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main()
{
	int year, month = 1, day = 0;
	int i, m; 
	while(scanf("%d", &m) != EOF){
		while(m--){
			scanf("%d%d%d", &year, &month, &day);
			if(day + 1 > day_month[month][isLeap(year)]){//超过当月天数
				month++;
				day = 1;//日期变为下个月的1号
			}
			else{
				day++;
			}
			if(month == 13){//满十二个月
				year++;
				month=1;//日期变为下一年的1月
			}
			printf("%04d-%02d-%02d\n", year, month, day);
			month = 1, day = 0;//重新初始化 
		}
	}
	return 0;
}

问题 E: 日期累加

题目描述

设计一个程序能计算一个日期加上若干天后是什么日期。

输入

输入第一行表示样例个数m,接下来m行每行四个整数分别表示年月日和累加的天数。

输出

输出m行,每行按yyyy-mm-dd的个数输出。

样例输入

1
2008 2 3 100

样例输出

2008-05-13

提示

题解

#include <cstdio>
#include <string.h>
int day_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}
};
bool isLeap(int year){//判断是否是闰年
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
int main()
{
	int year, month = 1, day = 0, num;
	int i, m; 
	while(scanf("%d", &m) != EOF){
		while(m--){
			scanf("%d%d%d%d", &year, &month, &day, &num);
			while(num > 0){
				day++;
				if(day == day_month[month][isLeap(year)] + 1){//满当月天数
					month++;
					day = 1;//日期变为下个月的1号
				}
				if(month == 13){//满十二个月
					year++;
					month=1;//日期变为下一年的1月
				}
				num--;
			}
			printf("%04d-%02d-%02d\n", year, month, day);
			month = 1, day = 0;//重新初始化 
		}
	}
	return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值