C++实现日期相关OJ题

x33g5p2x  于2021-10-28 转载在 C/C++  
字(3.9k)|赞(0)|评价(0)|浏览(358)

日期差值

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

输入描述

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

输出描述

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

示例1

输入:

20110412
20110422

输出:

11

解题代码

#include<iostream>
using namespace std;
class Date
{
public:
	// 获取某年某月的天数
	int GetMonthDay(int year, int month)
	{
		//下标对应月,数据是当月天数
		int MonthDays[] = {0,31,28,31,30,31,30,31,31,30,31,30,31 };
		//闰年的话,二月加一天
		if (month == 2 && ((year%4 == 0 && year%100 != 0)||year % 400 == 0))
			MonthDays[2]++;
		return MonthDays[month];
	}
	// 全缺省的构造函数
	Date(int year = 1900, int month = 1, int day = 1)
		:_year(year),_month(month),_day(day)
	{}	
	bool operator < (const Date& d)
	{
		if (_year < d._year)
			return true;
		else if (_year == d._year && _month < d._month)
			return true;
		else if (_year == d._year && _month == d._month && _day < d._day)
			return true;
		else
			return false;
	}
    	// ==运算符重载
	bool operator==(const Date& d)
	{
		return _year == d._year && _month == d._month && _day == d._day;
	}
    // !=运算符重载
	bool operator != (const Date& d)
	{
		return !(*this == d);
	}
    // 日期+=天数
	Date& operator+=(int day)
	{
			_day += day;

			while (_day > GetMonthDay(_year, _month))
			{
				_day -= GetMonthDay(_year, _month);
				_month++;
				if (_month == 13)
				{
					//年进位
					_year++;
					_month = 1;//月更新
				}
			}
			return *this;
	}

// 日期-日期 返回天数
	int operator-(const Date& d)
	{
		//找出大的日期和小的日期
		Date max = *this;
		Date min = d;
		if (*this < d)
		{
			max = d;
            min = *this;

		}
		int count = 0;
		while (max != min)
		{
			min+=1;
			count++;
		}
		return count+1;
	}

private:
	int _year;
	int _month;
	int _day;
};
int main() 
{
    int date1,date2;
    int year1,month1,day1,year2,month2,day2;
    while (cin>>date1>>date2)
    {
        year1 = date1 / 10000, month1 = date1 % 10000 / 100, day1 = date1 % 100;
        year2 = date2 / 10000, month2 = date2 % 10000 / 100, day2 = date2 % 100;
        Date d1(year1, month1, day1);
        Date d2(year2, month2, day2);
        cout<<(d1-d2)<<endl;
    }
    return 0;
}

计算日期到天数转换

题目描述
根据输入的日期,计算是这一年的第几天。保证年份为4位数且日期合法。

输入描述

输入一行,每行空格分割,分别是年,月,日

输出描述

输出是这一年的第几天

示例1

输入:

2012 12 31

输出:

366

解题代码

#include<iostream>
using namespace std;
int main()
{
    //monthDays[i]存的是1+2+......+i月的天数
    int monthDays[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
    int year,month,day;
    cin>>year>>month>>day;
    int n=monthDays[month-1]+day;
    //大于二月并且是闰年:
    if(month>2&&((year%4==0 && year%100!=0) || year %400 == 0))
        n++;
    cout<<n<<endl;
    return 0;
}

求1+2+3+…+n

题目描述
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

输入描述

输入1+2+3…+n中的n是多少

返回值描述

返回是1+2+3+…+n的值

示例1

输入:

5

返回值:

15

解题代码

class Solution 
{
public:
    
    int Sum_Solution(int n) 
    {
        Sum a[n];
        return _ret;
    }
private:
    class Sum//内部类,天生就是Solution的友元类,可以访问Solution的私有
    {
    public:
        Sum()
        {
            _ret+=_i;
            _i++;
        }
    };
    static int _i;
    static int _ret;

};
int Solution::_i=1;
int Solution::_ret=0;

日期累加

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

输入描述

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

输出描述

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

示例1

输入:

1
2008 2 3 100

输出:

2008-05-13

解题代码

#include<iostream>
using namespace std;
// 获取某年某月的天数
int GetMonthDay(int year, int month)
{
	//下标对应月,数据是当月天数
	int MonthDays[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	//闰年的话,二月加一天
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
		MonthDays[2]++;
	return MonthDays[month];
}
int main()
{

	// 日期+=天数
	int year, month, day, days;
    int m = 0;
    cin>>m;
    while(m--)
    {
        cin >> year >> month >> day >> days;
        
		if (days > 0)
		{
			day += days;

			while (day > GetMonthDay(year, month))
			{
				day -= GetMonthDay(year, month);
				month++;
				if (month == 13)
				{
					//年进位
					year++;
					month = 1;//月更新
				}
			}
		    printf("%04d-%02d-%02d\n",year,month,day);

		}
		else
		{
			day += days;
			while (day <= 0)
			{
				month--;
				if (month == 0)
				{
					month = 12;
					year--;
				}
				day += GetMonthDay(year, month);
			}
		    printf("%04d-%02d-%02d\n",year,month,day);
		}
    }
	
	return 0;
}

打印日期

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

输入描述

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

输出描述

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

示例1

输入:

2000 3
2000 31
2000 40
2000 60
2000 61
2001 60

输出:

2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01

解题代码

#include<iostream>
using namespace std;
int main()
{
    int year,num;
    while(cin>>year>>num)
    {
        //下标对应月份,元素就是该月的天数
        int monthDays[]={0,31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int month = 1;//控制月份
        while(num>monthDays[month])
        {
            if((year%4==0 && year%100!=0)||year%400 == 0)
            {
                monthDays[2]=29;
            }
            num-=monthDays[month];//减去该月的天数
            month++;
        }
        printf("%04d-%02d-%02d",year,month,num);
    }
    return 0;
}

相关文章

微信公众号

最新文章

更多