题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include <array>
#include <iostream>
using namespace std;
int main() {
int days = 0;
string year, month, day;
cin >> year >> month >> day;
/*一个年份如果能被4整除但不能被100整除,则是闰年。
一个年份如果能被400整除,也是闰年。
例如,2024年是闰年,因为它能被4整除且不能被100整除。而1900年不能被400整除,所以不是闰年。*/
array<int, 12> months{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool bistrue = (stoi(year) % 4 == 0 && stoi(year) % 100 != 0);
for(int i = 0; i < stoi(month)-1; ++i){
days += months[i];
}
days += stoi(day);
/*闰年天数加一*/
if(bistrue)
days += 1;
cout << days << endl;
}
// 64 位输出请用 printf("%lld")

