巩固类和对象的知识点——牛客5道题目
文章目录
- JZ64求1+2+3+...+n
- 思路
- 代码
- HJ73计算日期到天数转换
- 思路
- 代码
- KY111日期差值
- 思路
- 代码
- KY222打印日期
- 思路
- 代码
- KY258日期累加
- 思路
- 代码
JZ64求1+2+3+…+n
思路
从题目的要求中可以看出,不能使用循环,分支语句,也不能使用位运算符,不能使用递归。
学习了c++之后,我们知道每次进行对象实例化的时候,都会调用构造函数,我们利用这个特性。在实现一个类,类的成员为静态的(因为要求和,要保留上一次的构造)。静态的成员不能在构造函数中定义。只能在全局定义。其次我们会访问该类的私有成员,所以需要把class Solution
这个类设置为友元类。
代码
class A
{
friend class Solution;
public:
A()
{
_sum+=_i;
_i++;
}
private:
static int _sum;
static int _i;
};
int A::_sum=0;
int A::_i=1;
class Solution {
public:
int Sum_Solution(int n) {
A sum[n];//这里运用了柔性数组
return A::_sum;
}
};
HJ73计算日期到天数转换
思路
年的总天数都是相同的(闰年多一天)。我们可以用一个数组记录从一月到每个月的累计天数。根据前一个月的累计天数加上该月的天数即为这一年的第几天。(注意闰年的二月)
代码
#include <iostream>
using namespace std;
int main() {
int year, month, day;
cin >> year >> month >> day;
int y_day[13] = {0,31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
if (month > 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
cout << y_day[month-1] + day + 1;
else
cout << y_day[month-1] + day;
}
KY111日期差值
思路
这个的日常差值,我用的是每天每天进行累计的。直到加到和另一个日期相等。重载了+,-运算符。
代码
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year, int month, int day)
:_year(year)
, _month(month)
, _day(day)
{}
Date& operator+(int m)
{
_day += m;
while (_day > judge(_year, _month))
{
_day -= judge(_year, _month);
_month++;
if (_month == 13)
{
_year++;
_month = 1;
}
}
return *this;
}
int judge(int year, int month)
{
static int M[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 M[month];
}
bool operator==(Date& A)
{
if (_year != A._year)
return false;
else if (_year == A._year && _month != A._month)
return false;
else if (_year == A._year && _month == A._month && _day != A._day)
return false;
else
return true;
}
int operator-(Date A)
{
int count = 1;
while (!(A == (*this)))
{
A + 1;
count++;
}
return count;
}
private:
int _year;
int _month;
int _day;
};
int main() {
int a, b;
while (cin >> a >> b) {
Date x(a / 10000, a % 10000 / 100, a % 100);
Date y(b / 10000, b % 10000 / 100, b % 100);
cout << y - x;
}
}
KY222打印日期
思路
思路非常简单,就是天数从第一个月开始减,直到天数小于该月的时候截至。主要要注意减2月份的时候——闰年29。
代码
没有用类和对象的知识。
#include <iostream>
using namespace std;
int judge(int year)
{
if((year%4==0&&year%100!=0)||year%400==0)
return 1;
return 0;
}
int main() {
int y, n;
while (cin >> y >> n) {
static int y_day[13] = {0, 31,28,31,30,31,30,31,31,30,31,30,31};
int i ;
for (i = 1; i <=12; i++) {
if (n > y_day[i]) {
n -= y_day[i];
if(i==2&&judge(y))
{
n--;
if(n==0)
{
i=2;
n=29;
printf("%d-%02d-%02d\n", y,i,n);
break;
}
}
}
else
{
printf("%d-%02d-%02d\n", y,i,n);
break;
}
}
}
}
类和对象的写法
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year)
:_year(year)
,_month(1)
,_day(0)
{}
Date& operator+(int day)
{
_day+=day;
while(_day>judge(_year,_month))
{
_day-=judge(_year,_month);
_month++;
}
return *this;
}
int judge(int year,int month)
{
static int y_day[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 y_day[month];
}
void Print()
{
printf("%d-%02d-%02d\n", _year,_month,_day);
}
private:
int _year;
int _month;
int _day;
};
int main() {
int a, b;
while (cin >> a >> b) { // 注意 while 处理多个 case
Date A(a);
(A+b).Print();
}
}
KY258日期累加
思路
首先我们创建一个日期类。该类包含构造函数(为了初始化),运算符重载(加日期),打印(访问私有成员)。
对于加的日期我们需要进行调整,使它变成正常的日期。
当该日期大于该年月的日期,就需要减掉该年月的日常,同时月数加1,如果月数等于13,则修改成1,且年数加1。直到最后符合正常的日期为止。
代码
#include <iostream>
using namespace std;
class Date
{
public:
Date(int year,int month,int day)
{
_year=year;
_month=month;
_day=day;
}
Date& operator+(int m)
{
_day+=m;
while(_day>judge(_year, _month))
{
_day-=judge(_year,_month);
_month++;
if(_month==13)
{
_year++;
_month=1;
}
}
return *this;
}
int judge(int year,int month)
{
static int M[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 M[month];
}
void Print()
{
printf("%d-%02d-%02d\n",_year,_month,_day);
}
private:
int _year;
int _month;
int _day;
};
int main()
{
int m;
cin>>m;
while(m--)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
Date A(a,b,c);
(A+d).Print();
}
return 0;
}