题解 | #打印日期#

打印日期

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

#include <iostream>
using namespace std;
int GetMonthDay(int year,int month)
{
    int arr[13] = {-1, 31, 28, 31, 30, 31, 30,
                      31, 31, 30, 31, 30, 31};
    if((month==2)&&((year%4==0 && year%100!=0)||(year %400 ==0)))
    {
        return 29;
    }
    return arr[month];
}
void fun(int& year, int& month, int& day) {
    while (day > GetMonthDay(year, month) ) {
        day -= GetMonthDay(year, month);
        ++month;
    }
}
int main() {
    int a, b;
    while (cin >> a >> b) { // 注意 while 处理多个 case
        int month = 1;
        fun(a, month, b);
        printf("%4d%c%02d%c%02d\n", a, '-', month, '-', b);
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-20 14:14
点赞 评论 收藏
分享
在开会的单身狗很有一套:学院本被想着这么快有面试,而且简历废话太多了 那些在校经历什么荣誉什么的企业不关心
点赞 评论 收藏
分享
秋招不是要开始了吗,我都打算润了,看大家还在找不敢润了
一条从:因为不是人人都像佬一样有实习像我们这种二本仔秋招没有实习也是白忙活
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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