文章目录 一、题目描述二、题解思路三、程序实例 一、题目描述 二、题解思路 将年月日提取出来;加上天数;加上相应前一个月的天数;判断是否为闰年,是就加1; 三、程序实例 class Solution { public: int dayOfYear(string date) { int year = atoi(date.substr(0, 4).c_str()); int month = atoi(date.substr(5, 2).c_str()); int day = atoi(date.substr(8, 2).c_str()); int d[] = {31,28,31,30,31,30,31,31,30,31,30,31}; int days = 0; days += day; for(int m = 1; m < month; ++m) { days += d[m -1]; } int iIsLeapYear = ((year % 4 != 0) ? 0 : ((year % 100 != 0) ? 1 : ((year % 400 == 0) ? 1 : 0))); return (month <= 2) ? days : iIsLeapYear + days; } };