题解 | #打印日期#
打印日期
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")
查看8道真题和解析
