题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <iostream> using namespace std; int daytab[2][13] = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } }; bool IsLeapYear(int year) { //判断是否为闰年 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int main() { int year, month, day; while (cin >> year >> month >> day) { int number = 0; //记录天数 int row = IsLeapYear(year); for (int i = 0; i < month; i++) { number += daytab[row][i]; } number += day; cout << number; } return 0; }