题解 | #打印日期#
打印日期
https://www.nowcoder.com/practice/b1f7a77416194fd3abd63737cdfcf82b
#include <stdio.h> #define bool int #define TRUE 1 #define FALSE 0 int mDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; int main() { int m, n; //年份和第几天 while (scanf("%d %d", &m, &n) != EOF) { // 注意 while 处理多个 case int month,day; bool isLeapYear = FALSE; //判断是不是闰年 if(m % 400 == 0 || m % 100 != 0 && m % 4 == 0){ isLeapYear = TRUE; mDays[2] = 29; }else{ mDays[2] = 28; } int days = 0; for(int i = 0; i < 13; i++){ days += mDays[i]; //如果刚好是某个月的最后一天 if(days == n){ month = i; day = mDays[i]; break; } //超过了天数 if(days > n){ month = i; day = n - (days - mDays[i]); break; } } //最后要格式化处理一下 if(day < 10 && month < 10){ printf("%d-0%d-0%d\n", m, month, day); }else if(day < 10){ printf("%d-%d-0%d\n", m, month, day); }else if(month < 10){ printf("%d-0%d-%d\n", m, month, day); }else{ printf("%d-%d-%d\n", m, month, day); } } return 0; }