# C++：OJ练习(每日练习！)

## 编程题：

### 题一：计算日期到天数的转换

2012 12 31

366

#### 思路一：

#include <iostream>
using namespace std;
int main()
{
int _year,_month,_day;
cin>>_year>>_month>>_day;
int sum = _day;
int arr[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int n = _month;
while(--n)
{
sum += arr[n];
}
if(_month > 2 && ((_year % 4 == 0 && _year % 100 != 0) || (_year % 400 == 0)))
sum += 1;
cout<<sum<<endl;
return 0;
}

#### 思路二：

#include <iostream>
using namespace std;
class Date
{
public:
//构造函数
Date(int year = 1,int month = 1,int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//计算当前年月所对应的天数
int GetMonth(int& year,int& month)
{
int arr[13] ={0,31,28,31,30,31,30,31,31,30,31,30,31};
if(month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
return arr[month];
}
//友元函数声明
friend ostream& operator<<(ostream& out,Date& d);
friend istream& operator>>(istream& out,Date& d);

private:
int _year;
int _month;
int _day;
};
//赋值运算符重载
ostream& operator<<(ostream& out,Date& d)
{
int sum = d._day;
--d._month;
while(d._month >0)
{
sum += d.GetMonth(d._year, d._month);
--d._month;
}

out<<sum<<endl;
return out;
}
istream& operator>>(istream& in,Date& d)
{
in>>d._year>>d._month>>d._day;
return in;
}
int main()
{
Date d1;
cin>>d1;
cout<<d1<<endl;
return 0;
}

### 题二：日期差值

20110412

20110422

11

#### 思路一：

需要定义：一个构造函数、对应月份天数、判断年月日是否相同、判断年月日大小、天数自增、计算天数差、赋值运算符重载‘ >> ’ ‘ << ’。

#include <iostream>
using namespace std;
#include <stdbool.h>
class Date
{
public:
//构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//对应的月份天数
int GetMonth(int& year, int& month)
{
int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
return arr[month];
}
//判断年月日是否相同
bool operator!=(Date& d)
{
return !(_year == d._year && _month == d._month && _day == d._day);
}
//判断年月日大小
bool operator<(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;
}
return false;
}
//天数自增
Date& operator++()
{
++_day;
if (_day > GetMonth(_year, _month))
{
_day = _day - GetMonth(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
return *this;
}
//计算天数差
int operator-(Date& d)
{
Date max = *this;
Date min = d;
if (*this < d)
{
max = d;
min = *this;
}
int n = 0;
while (min != max)
{
++min;
++n;
}
return n + 1;
}
//友元声明
friend ostream& operator<<(ostream& out, Date& d);
friend istream& operator>>(istream& out, Date& d);
private:
int _year;
int _month;
int _day;
};
//赋值运算符重载
ostream& operator<<(ostream& out, Date& d)
{
out << d._year << d._month << d._day << endl;
return out;
}
istream& operator>>(istream& in, Date& d)
{
scanf("%4d%2d%2d", &d._year, &d._month, &d._day);
return in;
}
int main()
{
Date d1;
Date d2;
cin >> d1;
cin >> d2;
cout << d1 - d2;
return 0;
}

#### 思路二：

#include <iostream>
using namespace std;
//平年从1月到n月的天数
int mon[12] = { 0,31,59,90,120,151,181,212,243,273,304,334 };
//给出年月日，计算距离0000年0月1日的天数和
int CountDay(int y, int m, int d)
{
// 计算0-y年的天数
int yearDay = y * 365 + y / 4 - y / 100 + y / 400;
// 计算到0-m月的天数
int monthDay = mon[m - 1];
if (m > 2 && ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0))
monthDay += 1;
return yearDay + monthDay + d;
}
int main()
{
int year1, month1, day1;
scanf("%4d%2d%2d", &year1, &month1, &day1);
int n1 = CountDay(year1, month1, day1);
int year2, month2, day2;
scanf("%4d%2d%2d", &year2, &month2, &day2);
int n2 = CountDay(year2, month2, day2);
cout << abs(n1 - n2) + 1 << endl;
return 0;
}

### 题三：打印日期

2000 3

2000 31

2000 40

2000 60

2000 61

2001 60

2000-01-03

2000-01-31

2000-02-09

2000-02-29

2000-03-01

2001-03-01

#### 思路一：

需要定义：一个构造函数、计算对应月份天数、规范天数、赋值运算符重载‘ >> ’ ‘ << ’的函数。

#include <iostream>
using namespace std;
class Date
{
public:
//构造函数
Date(int year = 1, int month = 1, int day = 1)
{
_year = year;
_month = month;
_day = day;
}
//计算对应月份的天数
int GetMonth(int& year, int& month)
{
int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
return arr[month];
}
//规范天数
void Calendar()
{
while (_day > GetMonth(_year, _month))
{
_day = _day - GetMonth(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
}
//友元声明
friend ostream& operator<<(ostream& out, Date& d);
friend istream& operator>>(istream& out, Date& d);
private:
int _year;
int _month;
int _day;
};
//赋值运算符重载
ostream& operator<<(ostream& out, Date& d)
{
printf("%04d-%02d-%02d", d._year, d._month, d._day);
return out;
}
istream& operator>>(istream& in, Date& d)
{
scanf("%4d%d", &d._year, &d._day);
return in;
}
int main()
{
Date d1;
cin >> d1;
d1.Calendar();
cout << d1;
return 0;
}

#### 思路二：

#include <iostream>
using namespace std;
int main()
{
int year;
int day;
int mon[13] = { 31, 28, 31, 30, 31, 30, 31, 31, 30 ,31, 30, 31 };
while (cin >> year >> day)
{
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
mon[1] = 29;
else
mon[1] = 28;
for (int i = 0; i < 12; i++)
{
if (day <= mon[i])
{
printf("%04d-%02d-%02d\n", year, i + 1, day);
break;
}
else
{
day = day - mon[i];
}
}
}
return 0;
}

### 题四：日期累加

1

2008 2 3 100

2008-05-13

#### 思路一：

需要定义：一个构造函数、计算对应月份天数、规范天数、赋值运算符重载‘ >> ’ ‘ << ’的函数。

#include <iostream>
using namespace std;
class Date
{
public:
Date(int year = 1, int month = 1, int day = 1,int sky = 0)
{
_year = year;
_month = month;
_day = day;
_sky = sky;
}
int GetMonth(int& year, int& month)
{
int arr[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
{
return 29;
}
return arr[month];
}
void Calendar()
{
_day = _day + _sky;
while (_day > GetMonth(_year, _month))
{
_day = _day - GetMonth(_year, _month);
++_month;
if (_month == 13)
{
++_year;
_month = 1;
}
}
}
friend ostream& operator<<(ostream& out, Date& d);
friend istream& operator>>(istream& out, Date& d);
private:
int _year;
int _month;
int _day;
int _sky;
};
ostream& operator<<(ostream& out, Date& d)
{
printf("%04d-%02d-%02d", d._year, d._month, d._day);
return out;
}
istream& operator>>(istream& in, Date& d)
{
in>>d._year>>d._month>>d._day>>d._sky;
return in;
}
int main()
{
int n = 0;
cin>>n;
while(n--)
{
Date d1;
cin>>d1;
d1.Calendar();
cout<<d1<<endl;;
}
return 0;
}
// 64 位输出请用 printf("%lld")

#### 思路二：

#include<iostream>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
int y, m, d, num;
int days[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
cin >> y >> m >> d >> num;
d += num;
while (d > days[m - 1])
{
if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
days[1] = 29;
else
days[1] = 28;
d -= days[m - 1];
if (d == 0)
d = 1;
m++;
if (m == 13)
{
y++;
m = 1;
}
}
printf("%4d-%02d-%02d\n", y, m, d);
}
}
return 0;
}

## 感谢大佬们的一键三连！ 感谢大佬们的一键三连！ 感谢大佬们的一键三连！

|
4月前
|

38 0
|
3月前
|
C++ 容器
C++之评委打分案例（vector与deque容器练习）
C++之评委打分案例（vector与deque容器练习）
27 1
|
3月前
|
C++
C++小练习：猜数游戏
C++小练习：猜数游戏
26 3
|
4月前
|

45 2
|
4月前
|

49 7
|
4月前
|
C语言

41 1
|
4月前
|

30 1
|
4月前
|

15 0
|
4月前
|

143 1
|
4月前
|

32 0