【C++初阶】有关日期的编程题

在这里插入图片描述

👦个人主页:@Weraphael
✍🏻作者简介:目前学习C++和算法
✈️专栏:C++航路
🐋 希望大家多多支持,咱一起进步!😁
如果文章对你有帮助的话
欢迎 评论💬 点赞👍🏻 收藏 📂 加关注✨


一、计算日期到天数转换

题目链接:点击跳转

在这里插入图片描述

#include <ctime>
#include <iostream>
#include <array>
using namespace std;

int Getday(int year, int month)
{
   
    array<int, 13> days = {
   0, 31, 28, 31, 30, 31,
                     30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && year % 400 == 0)
    {
   
        return 29;
    }
    if (month == 2 && year % 4 == 0 && year % 100 != 0)
    {
   
        return 29;
    }
    else 
    {
   
        return days[month];
    }
}

int main() 
{
   
    int year, month, day;
    cin >> year >> month >> day;

    // 遍历 1月 ~ month(不包括month)
    // 然后再加上当前month的天数即可
    // 注意还要判断当前年是否是闰年
    int ans = 0;
    for (int i = 1; i < month; i++)
    {
   
        ans += Getday(year, i);
    }
    ans += day;
    cout << ans << endl;
    
    return 0;
}

二、日期差值

题目链接:点击跳转

在这里插入图片描述

#include <iostream>
#include <string>
#include <array>
using namespace std;

int Getday(int year, int month)
{
   
    array<int, 13> days = {
   0, 31, 28, 31, 30, 31,
                     30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && year % 400 == 0)
    {
   
        return 29;
    }
    if (month == 2 && year % 4 == 0 && year % 100 != 0)
    {
   
        return 29;
    }
    else 
    {
   
        return days[month];
    }
}

bool leap_year(int year)
{
   
	if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
	{
   
		return true;
	}
	else 
	{
   
		return false;
	}
}

int main()
{
   
	int y1, m1, d1;
    int y2, m2, d2;
	// 计算两个日期相对于0001 01 01的差值
	// 然后差值相减取绝对值+1就可以了
    while (scanf("%4d%2d%2d%4d%2d%2d", &y1, &m1, &d1, &y2, &m2, &d2) != EOF)
	{
   
		// 计算y1, m1, d1
		int ans1 = 0;
		for (int i = 1; i < y1; i++)
		{
   
			if (leap_year(i))
				ans1 += 366;
			else ans1 += 365;
		}
		for (int i = 1; i < m1; i++)
		{
   
			ans1 += Getday(y1, i);
		}
		ans1 += d1;

		// 计算y2, m2, d2
		int ans2 = 0;
		for (int i = 1; i < y2; i++)
		{
   
			if (leap_year(i))
				ans2 += 366;
			else ans2 += 365;
		}
		for (int i = 1; i < m2; i++)
		{
   
			ans2 += Getday(y2, i);
		}
		ans2 += d2;
		
		cout << abs(ans1 - ans2) + 1 << endl;
	}
	return 0;
}

三、打印日期

题目链接:点击跳转

在这里插入图片描述

#include <iostream>
#include <array>
using namespace std;

int Getday(int year, int month)
{
   
    array<int, 13> days = {
   0, 31, 28, 31, 30, 31,
                     30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && year % 400 == 0)
    {
   
        return 29;
    }
    if (month == 2 && year % 4 == 0 && year % 100 != 0)
    {
   
        return 29;
    }
    else 
    {
   
        return days[month];
    }
}

int main()
{
   
    int year, x;
    while (cin >> year >> x)
    {
   
        // 最低只有1月
        int month = 1;
        while (x > Getday(year, month))
        {
   
            x -= Getday(year, month);
            ++month;

            if (month == 13)
            {
   
                ++year;
                month = 1;
            }
        }
        printf("%04d-%02d-%02d\n", year, month, x);
    }
    return 0;
}

四、累加天数

题目链接:点击跳转

在这里插入图片描述

#include <iostream>
#include <array>
using namespace std;

int Getday(int year, int month)
{
   
    array<int, 13> days = {
   0, 31, 28, 31, 30, 31,
                     30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && year % 400 == 0)
    {
   
        return 29;
    }
    if (month == 2 && year % 4 == 0 && year % 100 != 0)
    {
   
        return 29;
    }
    else 
    {
   
        return days[month];
    }
}

int main()
{
   
    int m;
    cin >> m;

    while (m--)
    {
   
        int year, month, day, x;
        cin >> year >> month >> day >> x;

        day += x;
        while (day > Getday(year, month))
        {
   
            day -= Getday(year, month);
            ++month;
            if (month == 13)
            {
   
                year++;
                month = 1;
            }
        }
        printf("%04d-%02d-%02d\n", year, month, day);
    }
    return 0;
}

相关推荐

  1. C++-string使用

    2024-02-18 06:50:02       39 阅读
  2. C++中模板(

    2024-02-18 06:50:02       22 阅读
  3. C++】模板

    2024-02-18 06:50:02       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-18 06:50:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-18 06:50:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-18 06:50:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-18 06:50:02       20 阅读

热门阅读

  1. python中线程/线程池,进程/进程池的创建

    2024-02-18 06:50:02       32 阅读
  2. 学习Android的第十四天

    2024-02-18 06:50:02       31 阅读
  3. 【无标题】

    2024-02-18 06:50:02       32 阅读
  4. ABC341A-D题解

    2024-02-18 06:50:02       30 阅读
  5. 寿司转盘,用 C 编码

    2024-02-18 06:50:02       32 阅读
  6. Pytorch的安装教程,解决jupyter不能使用pytorch的问题

    2024-02-18 06:50:02       33 阅读
  7. 每日OJ题_算法_递归⑤力扣50. Pow(x, n)

    2024-02-18 06:50:02       37 阅读
  8. JVM的类的生命周期

    2024-02-18 06:50:02       30 阅读
  9. 2024年五大科技与创业趋势:从AI退热到IPO挑战

    2024-02-18 06:50:02       29 阅读