题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <stdio.h> int day_of_year(int year, int month, int day); int main(){ int year, month, day; int serial = 1; while( scanf("%d %d %d", &year, &month, &day) != EOF ){ serial = day_of_year(year, month, day); printf("%d\n", serial); } return 0; } int day_of_year(int year, int month, int day) { int ret = 0; //每个月之前的累计天数 int LeapYear[12] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}; int NormalYear[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //闰年 if( (year%4==0 && year%100 != 0) || year%400 == 0 ) ret = LeapYear[month-1] + day; //平年 else ret = NormalYear[month-1] + day; return ret; }