Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 1.54 KB

1931.md

File metadata and controls

76 lines (56 loc) · 1.54 KB

1931题目描述

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

输入

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

输出

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

  • 样例输入

2013 60

2012 300

2011 350

2000 211

  • 样例输出

2013-03-01

2012-10-26

2011-12-16

2000-07-29

思路

先判断,输入年份是否是闰年,

然后用输入天数,从一月开始减去当月的天数,直到day的值小于等于0为止.

减去第m个月天数day<=0,日期就是m月,

(m月的天数)month[m][Isleap(year)]+day(day <= 0) ,就是对应的号数

#include <iostream>
using namespace std;
bool Isleap(int year){
    return (year % 4 == 0 && year %100 !=0 || year % 400 == 0);
}
int main() {
    int month[13][2] = {{0,  0},
                    {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 year, day;
    while (scanf("%d %d", &year, &day) != EOF) {
        bool k = Isleap(year);
        int m;
        for (m = 1; m <= 12; m++) {
            day -= month[m][k];
            if (day <= 0) break;
        }
        int d = day + month[m][k];
        printf("%.4d-%.2d-%.2d\n",y, m, d);
    }
    return 0;
}