此题较为简单,直接上代码:
int dayOfYear(string date) {
int year = (date[0] - '0')*1000;
year += (date[1] - '0')*100;
year += (date[2] - '0')*10;
year += (date[3] - '0');
//cout << year;
int month = (date[5] - '0')*10;
month += (date[6] - '0');
//cout << month;
int Data = (date[8] - '0')*10;
Data += (date[9] - '0');
//cout << Data;
int run = 0;
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
run = 1;
}
int m[13];
m[0] = 0,m[1]= 31,m[2] = m[1] + 28 + run,m[3] = m[2] + 31,m[4] = m[3] + 30,m[5] = m[4] + 31,m[6] = m[5] + 30,m[7] = m[6] + 31,m[8] = m[7] + 31,m[9] = m[8] + 30,m[10] = m[9] + 31,m[11] = m[10] + 30,m[12] = m[11] + 31;
int result = 0;
month++;
result += m[month - 2] + Data;
return result;
}