题解 | 日期累加

日期累加

https://www.nowcoder.com/practice/eebb2983b7bf40408a1360efb33f9e5d

#include <stdio.h>
//难点在于:1 闰年判断(4,100x;400) 2 年月日进制转换 3 改变后重新判断闰年和相应计算
int n[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int r[13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//闰年

int main() {
    int m;
    scanf("%d", &m);

    for (int case_num = 0; case_num < m; case_num++) { // 修复变量名冲突
        int y, mo, d, b,t=0;
        scanf("%d%d%d%d", &y, &mo, &d, &b);
        b += d;

        if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) { // 闰年
            int current_month = mo; // 引入新变量表示当前月份
            while (b > 0) {
                if (current_month > 12) {//年变
                    current_month = 1;
                    y++;
                    t=1;
                }
                int days_in_month = r[current_month]; // 使用闰年数组
                if(t)//注意年份改变后,判断是否为闰年
                {
                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                        days_in_month = r[current_month];
                       else  days_in_month = n[current_month];
                }
                if (b <= days_in_month) {
                    d = b;
                    mo = current_month;
                    printf("%04d-%02d-%02d\n", y, mo, d);
                    break;
                } else {
                    b -= days_in_month;
                    current_month++;
                }
            }
        } else { // 平年
            int current_month = mo;
            while (b > 0) {
                if (current_month > 12) {
                    current_month = 1;
                    y++;
                    t=1;
                }
                int days_in_month = n[current_month]; // 使用平年数组
                if(t)
                {
                    if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
                        days_in_month = r[current_month];
                       else  days_in_month = n[current_month];
                }
                if (b <= days_in_month) {
                    d = b;
                    mo = current_month;
                    printf("%04d-%02d-%02d\n", y, mo, d);
                    break;
                } else {
                    b -= days_in_month;
                    current_month++;
                }
            }
        }
    }

    return 0;
}

全部评论

相关推荐

06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
07-23 22:01
已编辑
梧州学院 Java
第一次面试,广东小厂,太紧张了,感觉机会不是很大,很多地方说得不好。面了二十分钟接下来是面试问的问题1.介绍一下项目,有什么功能太紧张了,回答有点卡壳2.了解的数据库有哪些,说一下MySQL的索引优化我说了一下索引的知识,但是没了解过索引优化,然后他让我自由发挥说一下我了解的MySQL知识,后面讲了数据库的事务,隔离级别,索引的b+树,太紧张,很多都没说出来,卡住了,让他下一个问题。3.spring&nbsp;cloud里面有哪些组件跟组件的作用讲了,nacos,sentinel,gateway,负载均衡,openfeign4.MinIO做文件存储,如果有十个g的上传,应该如何提高上传和下载的效率这个不会,没了解过,只说了用mq提高响应速度5.redis在项目中起到什么样的作用缓存热门信息,做排行榜,redis分布式锁做限制请求6.redis怎么保证跟数据库一致性这个答得不好7.怎么提高接口的响应速度只说了慢sql和mq8.怎么理解Java中的多态举例只说了重载跟重写9.介绍一下springioc和aop10.bean的生命周期这两个还可以八股问完了,我问面试评价,说我太紧张了,可以放松一些(确实第一次面试,听录音回放全程在鹅鹅鹅饿,感觉过不了),问我能不能接受出差,可能去深圳做技术支持,问我多久到岗,问他主要工作内容是负责开发吗,他说可能有部分时间不在公司要去出差。应该是挂了,叫我过两天等通知。
想当java高手:报喜,面试已经通过了,准备下周一入职,月薪3000
查看10道真题和解析
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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