题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <stdio.h> int main() { int year = 0; int month = 0; int data = 0; scanf("%d %d %d", &year, &month, &data); int day = data; //平年时,12个月的天数,闰年2月为29天 int month_day[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //判断为平年还是闰年 while(1) { //闰年 if((year%4 == 0 && year%100 != 0) || year%400 == 0) { if(month>2) { int i = 0; for(i=0; i<month; i++) { day += month_day[i]; } day += 1; } else if(month == 2) { day = 31 + data; } break; } //平年 else { int i = 0; for(i=0; i<month; i++) { day += month_day[i]; } break; } } printf("%d\n", day); return 0; }