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

原题链接

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;
}

posted @ 2022-08-14 10:05  r涤生  阅读(25)  评论(0)    收藏  举报