题解 | #打印日期#
打印日期
https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b
#include <cstdio> #include <iostream> using namespace std; int timevault[2][12] = {{31,29,31,30,31,30,31,31,30,31,30,31},{31,28,31,30,31,30,31,31,30,31,30,31}}; //致敬经典万智牌时之库 (Time Vault) int main() { int year, day; while (cin >> year >> day) { // 注意 while 处理多个 case // cout << a + b << endl; if (((year % 4 == 0)&&(year % 100 != 0))||year % 400 == 0) //分闰年处理 { int counter = 0; //月份 while (day > 0) { if (day - timevault[0][counter] > 0) { day -= timevault[0][counter] ; counter++; }else { break; //不够减了,月份已得出,退出循环。 } } printf("%4d-%02d-%02d\n",year,counter + 1,day); //因为数组缘故,counter比实际月份小1 }else { int counter = 0; while (day > 0) { if (day - timevault[1][counter] > 0) { day -= timevault[1][counter] ; counter++; }else { break; } } printf("%4d-%02d-%02d\n",year,counter + 1,day); } } } // 64 位输出请用 printf("%lld")