题解 | 计算日期到天数转换
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <array>
#include <iostream>
using namespace std;
bool is_leap_year(int y){//-----------判断闰年
if (y % 400 == 0) return 1;
if (y % 100 == 0) return 0;
if (y % 4 == 0) return 1;
return 0;
}
int main() {
int y, m, d;
cin >> y >> m >> d;
array<int, 12> a{31, 28, 31,30,31,30,31,31,30,31,30,31};//---------记录每月的天数
if (is_leap_year(y)) a[1] = 29;//-------------确定二月的天数
int ans = 0;
for (int i = 0; i < m - 1; i++)//---------计算当前月之前的天数
ans += a[i];
ans += d;//---------------加上当前月的天数
cout << ans;
return 0;
}
// 64 位输出请用 printf("%lld")
#写题解领奖励#