一、KY222题目
二、代码
#include <climits>
#include <iostream>
using namespace std;
class Date{
public:
Date(int year =1,int month =2,int day =3)
{
_year = year;
_month = month;
_day = day;
}
int GetDay(int year ,int month);
void Define(int n);
public:
int _year;
int _month;
int _day;
};
int Date::GetDay(int year ,int month)
{
static int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if(month == 2)
{
if((year%4==0 && year%100!=0 ) || year%400 == 0)
return 29;
}
return a[month];
}
void Date::Define(int n)
{
_day +=n;
while(_day>GetDay(_year,_month))
{
_day -=GetDay(_year,_month);
++_month;
if(_month == 13)
{
++_year;
_month =1;
}
}
}
int main() {
int year,num;
cin>>year>>num;
Date d(year,1,0);
d.Define(num);
printf("%04d-%02d-%02d",d._year,d._month,d._day);
return 0;
}
// 64 位输出请用 printf("%lld")
三、KY111题目
四、代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
class Date {
public:
Date(int year = 0, int month = 0, int day = 0) {
_year = year;
_month = month;
_day = day;
}
int GetDay(int year, int month);
bool operator<(const Date& d);
int D_value(Date d);//计算日期差
int Total_end();//某一年1月1日到某一天一共有多少天
int Total_year();//某一年的总天数
public:
int _year;
int _month;
int _day;
};
int Date::GetDay(int year, int month) {
const static int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if (month == 2) { //某年是闰年的时候,2月为29
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
return 29;
}
}
return a[month];
}
bool Date::operator<(const Date& d)
{
if (_year < d._year)
return true;
else if (_year == d._year && _month < d._month)
return true;
else if (_year == d._year && _month == d._month && _day < d._day)
return true;
else
return false;
}
int Date::Total_end()//某一年1月1日到某一天一共有多少天
{
int sum = _day;
while (--_month)
{
sum += GetDay(_year, _month);
}
return sum;
}
int Date::Total_year()//某一年的总天数
{
int sum = 0;
for (int i = 1; i <= 12; i++)
{
sum += GetDay(_year, i);
}
return sum;
}
int Date::D_value(Date d)
{
Date tmp(d);
if ((*this) < tmp)
{
if (_year == tmp._year)
{
return tmp.Total_end() - this->Total_end();
}
else
{
int d1 = this->Total_year() - this->Total_end();
int d2 = tmp.Total_end();
int x = _year + 1;
int d3 = 0;
while (x != tmp._year)
{
Date x_(x, 1, 1);
d3 += x_.Total_year();
x++;
}
return d1 + d2 + d3;
}
}
else//this大
{
if (_year == tmp._year)
{
return this->Total_end() - tmp.Total_end();
}
else
{
int d1 = tmp.Total_year() - tmp.Total_end();
int d2 = this->Total_end();
int x = tmp._year + 1;
int d3 = 0;
while (x != _year)
{
Date x_(x, 1, 1);
d3 += x_.Total_year();
x++;
}
return d1 + d2 + d3;
}
}
}
int main() {
int year_1, month_1, day_1;
int year_2, month_2, day_2;
int date1, date2;
while (scanf("%d%d", &date1, &date2) != EOF)
{
year_1 = date1 / 10000;
month_1 = (date1 / 100) % 100;
day_1 = date1 % 100;
year_2 = date2 / 10000;
month_2 = (date2 / 100) % 100;
day_2 = date2 % 100;
Date d1(year_1, month_1, day_1);
Date d2(year_2, month_2, day_2);
cout<<d1.D_value(d2)+1;//不加1时,相邻两天差1天
return 0;
}
}