题解 | 计算一年中的第几天

计算一年中的第几天

https://www.nowcoder.com/practice/178aa3dafb144bb8b0445edb5e9b812a

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

int main() {
    int year, month, day;
    vector<int> monthcount = {0,31,28,31,30,31,30,31,31,30,31,30 };
    for (int i = 1; i < 12; i++)
    {
        monthcount[i] += monthcount[i - 1];
    }
    while (cin >> year >> month >> day)
    {
        int count = 0;
        if (month <= 2)
        {
            count = monthcount[month - 1] + day;
            cout << count << endl;
            continue;
        }

        if (year % 4 == 0 || year % 400 == 0)
        {
            count += monthcount[month - 1] + day + 1;
            cout << count << endl;
        }
        else
        {
            count += monthcount[month - 1] + day;
            cout << count << endl;
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务