文章目录
机试之日期问题——打印日期
相关链接:
Contest100000578-《算法笔记》3.4小节——入门模拟->日期处理
题目
题目描述
给出年分m和一年中的第n天,算出第n天是几月几号。
输入描述:
输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。
输出描述:
可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。
示例1
输入:
2000 3
2000 31
2000 40
2000 60
2000 61
2001 60
输出:
2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01
代码
//日期问题——打印日期
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int monthDay[2][13] = {
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}
};
bool isLeap(int year){
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
return true;
}
else{
return false;
}
}
int main(){
int year,month,day,number;
while(cin>>year>>number){
month=0;
while(number > monthDay[isLeap(year)][month]){
number -= monthDay[isLeap(year)][month];
month++;
}
day = number;
printf("%04d-%02d-%02d\n",year,month,day);
}
return 0;
}