题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include<iostream>
using namespace std;
class Date {
public:
// 获取某年某月的天数
int GetMonthDay(int year, int month) {
int arr[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
//闰年29天
if (arr[month] == 28
&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))) {
return 29;
}
return arr[month];
}
//获取一年的天数
int GetYearDay(int year, int month, int day) {
int SumDay = 0;
for (int i = 1; i <= month - 1; i++) {
SumDay += GetMonthDay(year, i);
}
SumDay += day;
return SumDay;
}
private:
int _year;
int _month;
int _day;
};
int main()
{
int a=0, b=0,c=0;
Date d1;
cin>>a>>b>>c;
cout << d1.GetYearDay(a,b,c) << endl;
return 0;
}
海康威视公司福利 1082人发布
查看9道真题和解析