题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
计算日期到天数转换
/* 2022-09-28 07:35:54 提前定义好一个日期数组,然后2月且是闰年的时候特殊判断一下即可 */ #include <iostream> using namespace std; static int monthDay[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; bool isLeapYear(int y) { return (y %4 == 0 && y % 100 != 0 || y % 400 == 0); } int getDaysByYM(int y, int m, int d) { int days = 0; if(m > 2 && isLeapYear(y)) { monthDay[2] += 1; } for(int i = 0; i < m; ++i) { days += monthDay[i]; } days += d; return days; } int main() { int year, mon, day; cin >> year >> mon >> day; int days = 0; cout << getDaysByYM(year, mon, day) << endl; return 0; }