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

计算日期到天数转换

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

#include <iostream>
using namespace std;

//平年每一个月的天数
int data1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//闰年每一个月的天数
int data2[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool check(int x) {
    //用来判断年是不是闰年
    if ((x % 400 == 0) || (x % 4 == 0 && x % 100 != 0)) {
        return true;
    }
    return false;
}

int main() {
    int year, month, day;
    while (cin >> year >> month >> day) { // 注意 while 处理多个 case
        int ans = 0;
        if (check(year)) {
            for (int i = 0; i < month - 1; i++) {
                ans += data2[i];
            }
            ans += day;
        } else {
            for (int i = 0; i < month - 1; i++) {
                ans += data1[i];
            }
            ans += day;
        }

        cout << ans;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-15 17:24
点赞 评论 收藏
分享
LazyBreeze:项目尽量体现你对技术的理解和深度,不是说把中间件用一下就完事了,你项目里面提到集群和分布式,你真在服务器上部署过吗,感觉太假了,第二个项目说自己用了微服务的什么组件,只是用了没有自己的思考,很难让面试官注意到你的简历。针对某几个技术点自己多思考一下,考虑一下有没有别的替代方案,可以写一下,即使没有真的实现
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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