牛客网 打印日期

题目链接:https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b?tpId=40&tqId=21554&tPage=2&rp=2&ru=/ta/kaoyan&qru=/ta/kaoyan/question-ranking

题目描述

给出年分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

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <cstring>
 5 #include <stack>
 6 #include <cstdio>
 7 using namespace std;
 8 int n,m,i;
 9 int isRun(int x)
10 {
11     return x%400==0||(x%4==0&&x%100!=0);
12 }
13 int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
14 int main()
15 {
16     while(cin>>n>>m){
17         if(isRun(n)) a[2]=29;
18         else a[2]=28;
19         for(i=0;i<13;i++){
20             if(m>a[i]){
21                 m-=a[i];
22             }
23             else break;
24         }
25         printf("%d-%02d-%02d
",n,i,m);
26     }
27     return 0;
28 }
原文地址:https://www.cnblogs.com/wydxry/p/10683620.html