Jobdu_oj(九度) 打印日期 -- 9

打印日期 - 9

Time Limit: 1000MS
Memory Limit: 10000K

Description

给出年分m和一年中的第n天,算出第n天是几月几号。

Input

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

Output

可能有多组测试数据,对于每组数据,
yyyy-mm-dd的格式将输入中对应的日期打印出来。

Sample Input

2000 3
2000 31
2000 40
2000 60
2000 61
2001 60

Sample Output

2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01

Source

// main.cpp
// IDE: CLion
// Created by DariusYoung on 2020/2/24.
//
#include <cstdio>
using namespace std;
#define isleap(x) x%100 != 0 && x%4 == 0 || x % 400 == 0 ? 1:0
int daysOfMonth[12][2]{
        31,31,
        28,29,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31,
        31,31,
        30,30,
        31,31,
        30,30,
        31,31
};
int main() {
    int year,date;
    int month;
    while(scanf("%d%d",&year,&date)) {
        for (month = 0; month < 12; month++) {
            if (date < daysOfMonth[month][isleap(year)]) break;
            date -= daysOfMonth[month][isleap(year)];
        }
        month++;
        printf("%d-%02d-%02d\n", year, month, date);
    }
    return 0;
}

Test case

2020 40
2020-02-09
2020 61
2020-03-01
2019 61
2019-03-02

Summary

通过预处理,用空间换时间,用来存储每月多少天的数组类似Hash思想,不过不会产生冲突。这种预处理的方法需要注意的是用来存储预处理数据的结构需声明在函数外(设为全局变量),以防止函数栈空间不足的情况。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值