题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

//暴力算法
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int a;
    vector<int> date;
    while (cin >> a) { // 注意 while 处理多个 case
        date.push_back(a);
    }
    
    int num = 0;

    if (date[0] % 400 == 0 ||(date[0] % 4 ==0 && date[0] % 100 != 0))
    {
        if (date[1] < 10)
        {
            int month = date[1] % 10;
            if (month == 1)
            {
                num = date[2];
            }
            else if (month == 2)
            {
                num = 31 + date[2];
            }
            else if (month == 3)
            {
                num = 31 + 29 + date[2];
            }
            else if (month == 4)
            {
                num = 31 + 29 + 31 + date[2];
            }
            else if (month == 5)
            {
                num = 31 + 29 + 31 + 30 + date[2];
            }
            else if (month == 6)
            {
                num = 31 + 29 + 31 + 30 + 31 + date[2];
            }
            else if (month == 7)
            {
                num = 31 + 29 + 31 + 30 + 31 + 30 + date[2];
            }
            else if (month == 8)
            {
                num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + date[2];
            }
            else if (month == 9)
            {
                num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + date[2];
            }
        }
        else
        {
            int month = date[1] - 10;
            if (month == 0)
            {
                num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date[2];
            }
            else if (month == 1)
            {
                num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date[2];
            }
            else if (month == 2)
            {
                num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date[2];
            }
        }
    }
    else
    {
        if (date[1] < 10)
        {
            int month = date[1] % 10;
            if (month == 1)
            {
                num = date[2];
            }
            else if (month == 2)
            {
                num = 31 + date[2];
            }
            else if (month == 3)
            {
                num = 31 + 28 + date[2];
            }
            else if (month == 4)
            {
                num = 31 + 28 + 31 + date[2];
            }
            else if (month == 5)
            {
                num = 31 + 28 + 31 + 30 + date[2];
            }
            else if (month == 6)
            {
                num = 31 + 28 + 31 + 30 + 31 + date[2];
            }
            else if (month == 7)
            {
                num = 31 + 28 + 31 + 30 + 31 + 30 + date[2];
            }
            else if (month == 8)
            {
                num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + date[2];
            }
            else if (month == 9)
            {
                num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + date[2];
            }
        }
        else
        {
            int month = date[1] - 10;
            if (month == 0)
            {
                num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + date[2];
            }
            else if (month == 1)
            {
                num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + date[2];
            }
            else if (month == 2)
            {
                num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + date[2];
            }
        }
    }
    cout << num ;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

头像
11-06 10:58
已编辑
门头沟学院 嵌入式工程师
双非25想找富婆不想打工:哦,这该死的伦敦腔,我敢打赌,你简直是个天才,如果我有offer的话,我一定用offer狠狠的打在你的脸上
点赞 评论 收藏
分享
shtdbb_:还不错,没有让你做了笔试再挂你
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务