首页 > 其他分享 >AcWing3391.今年第几天?(日期题)

AcWing3391.今年第几天?(日期题)

时间:2022-08-14 10:24:48浏览次数:78  
标签:AcWing3391 第几天 int res 31 日期 mounth year day

原题链接

https://www.acwing.com/problem/content/3394/

日期题思路

满足下面条件之一的是闰年:

年份是 4 的整数倍,而且不是 100 的整数倍;
年份是 400 的整数倍。

处理输入

写个mounths数组

写判断是否是闰年函数

写判断某年某月多少天函数

计算到mounth-1有多少天,最后加上day

代码

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int mounths[] = {
    0,31,28,31,30,31,30,31,31,30,31,30,31
};

int is_leap(int year) // 判断是否是闰年
{
    if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return 1;
    return 0;
}

int get_days(int year,int mounth) // 计算year年mounth月有多少天
{
    int res = mounths[mounth];
    if(mounth == 2) res += is_leap(year);
    
    return res;
}

int main()
{
    int year,mounth,day;
    while(cin >> year >> mounth >> day)
    {
        int res = 0;
        for(int i = 1; i < mounth; i ++)
        {
            res += get_days(year,i);
        }
        res += day;
        cout << res << endl;
    }
    
    return 0;
}

标签:AcWing3391,第几天,int,res,31,日期,mounth,year,day
From: https://www.cnblogs.com/rdisheng/p/16584877.html

相关文章

  • [2016年NOIP普及组] 回文日期
    试题分析:本题是一道暴力枚举题,我们可以直接从输入的date1开始遍历到date2,其余的我们只需要判断是否超出日期即可。注意:没有00月与00日,这里需要单独判断。代码如下: ......