【每日刷题】Day81
🥕个人主页:开敲🍉
🔥所属专栏:每日刷题🍍
🌼文章目录🌼
1. 日期累加_牛客题霸_牛客网 (nowcoder.com)
2. 打印日期_牛客题霸_牛客网 (nowcoder.com)
3. 2956. 找到两个数组中的公共元素 - 力扣(LeetCode)
1. 日期累加_牛客题霸_牛客网 (nowcoder.com)
//注:此题思路仅供参考
//思路:实现日期类的功能:日期与日期间的大小判断、日期+=天数、日期-=天数、日期+天数、日期-天数、输入输出符的重载...
#include <iostream>
using namespace std;
class Date
{
public:
friend ostream& operator<<(ostream& out,const Date &d);
Date(int year = 0,int month = 0,int day = 0)
{
_year = year;
_month = month;
_day = day;
}
bool operator>(const Date& d);
bool operator==(const Date& d);
bool operator<(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);
bool operator!=(const Date& d);
Date& operator+=(int day);
Date& operator-=(int day);
Date operator+(int day);
private:
int _year;
int _month;
int _day;
};
//<<运算符重载
ostream& operator<<(ostream& out,const Date &d)
{
if((d._month<10&&d._day>10)||(d._month<10&&d._day==10))
out<<d._year<<"-"<<0<<d._month<<"-"<<d._day;
else if(d._month<10&&d._day<10)
out<<d._year<<"-"<<0<<d._month<<"-"<<0<<d._day;
else if((d._month>10&&d._day<10)||(d._month==10&&d._day<10))
out<<d._year<<"-"<<d._month<<"-"<<0<<d._day;
else
out<<d._year<<"-"<<d._month<<"-"<<d._day;
return out;
}
//>运算符重载
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;
return false;
}
//==运算符重载
bool Date::operator==(const Date &d)
{
return _year==d._year&&_month==d._month&&_day==d._day;
}
//<运算符重载
bool Date::operator<(const Date &d)
{
return !(*this>d||*this==d);
}
//>=运算符重载
bool Date::operator>=(const Date &d)
{
return !(*this<d);
}
//<=运算符重载
bool Date::operator<=(const Date &d)
{
return !(*this>d);
}
//!=运算符重载
bool Date::operator!=(const Date &d)
{
return !(*this==d);
}
//获取月份天数
int GetMonthDay(int year,int month)
{
int arr[13] = {-1,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];
}
//日期+=天数
Date& Date::operator+=(int day)
{
_day+=day;
while(_day>GetMonthDay(_year, _month))
{
_day-=GetMonthDay(_year, _month);
_month++;
if(_month==13)
{
_month = 1;
_year++;
}
}
return *this;
}
//日期-=天数
Date& Date::operator-=(int day)
{
_day-=day;
while(_day<=0)
{
_month--;
if(!_month)
{
_month = 12;
_year--;
}
_day+=GetMonthDay(_year, _month);
}
return *this;
}
//日期+天数
Date Date::operator+(int day)
{
Date tmp = *this;
tmp+=day;
return tmp;
}
int main()
{
int n = 0;
cin>>n;
int year,month,day,num;
while(n)
{
scanf("%4d %2d %2d %d",&year,&month,&day,&num);
Date d(year,month,day);
Date tmp = d+num;
cout<<tmp<<endl;
n--;
}
return 0;
}
2. 打印日期_牛客题霸_牛客网 (nowcoder.com)
//注:此题思路仅供参考
//思路:同上一题
#include <iostream>
using namespace std;
class Date
{
public:
friend ostream& operator<<(ostream& out,const Date &d);
Date(int year = 0,int month = 0,int day = 0)
{
_year = year;
_month = month;
_day = day;
}
bool operator>(const Date& d);
bool operator==(const Date& d);
bool operator<(const Date& d);
bool operator>=(const Date& d);
bool operator<=(const Date& d);
bool operator!=(const Date& d);
Date& operator+=(int day);
private:
int _year;
int _month;
int _day;
};
//<<运算符重载
ostream& operator<<(ostream& out,const Date &d)
{
if((d._month<10&&d._day>10)||(d._month<10&&d._day==10))
out<<d._year<<"-"<<0<<d._month<<"-"<<d._day;
else if(d._month<10&&d._day<10)
out<<d._year<<"-"<<0<<d._month<<"-"<<0<<d._day;
else if((d._month>10&&d._day<10)||(d._month==10&&d._day<10))
out<<d._year<<"-"<<d._month<<"-"<<0<<d._day;
else
out<<d._year<<"-"<<d._month<<"-"<<d._day;
return out;
}
//>运算符重载
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;
return false;
}
//==运算符重载
bool Date::operator==(const Date &d)
{
return _year==d._year&&_month==d._month&&_day==d._day;
}
//<运算符重载
bool Date::operator<(const Date &d)
{
return !(*this>d||*this==d);
}
//>=运算符重载
bool Date::operator>=(const Date &d)
{
return !(*this<d);
}
//<=运算符重载
bool Date::operator<=(const Date &d)
{
return !(*this>d);
}
//!=运算符重载
bool Date::operator!=(const Date &d)
{
return !(*this==d);
}
//获取月份天数
int GetMonthDay(int year,int month)
{
int arr[13] = {-1,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];
}
//日期+=天数
Date& Date::operator+=(int day)
{
_day+=day;
while(_day>GetMonthDay(_year, _month))
{
_day-=GetMonthDay(_year, _month);
_month++;
if(_month==13)
{
_month = 1;
_year++;
}
}
return *this;
}
int main()
{
int year,num;
while(scanf("%d %d",&year,&num)!=EOF)
{
Date d(year,1,1);
d+=(num-1);
cout<<d<<endl;
}
return 0;
}
3. 2956. 找到两个数组中的公共元素 - 力扣(LeetCode)
//思路1:二次遍历。
int* findIntersectionValues(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize)
{
int* ans = (int*)malloc(sizeof(int)*2);
int answer1 = 0;
int answer2 = 0;
*returnSize = 2;
//遍历数组1找相同
for(int i = 0;i<nums1Size;i++)
{
for(int j = 0;j<nums2Size;j++)
{
if(nums1[i]==nums2[j])
{
answer1++;
break;
}
}
}
//遍历数组2找相同
for(int i = 0;i<nums2Size;i++)
{
for(int j = 0;j<nums1Size;j++)
{
if(nums2[i]==nums1[j])
{
answer2++;
break;
}
}
}
ans[0] = answer1;
ans[1] = answer2;
return ans;
}
//思路2:哈希表。记录数组1、数组2分别由哪些元素组成,随后分别遍历数组1和数组2,判断当前元素是否在另一个数组中存在。
int* findIntersectionValues(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize)
{
int* ans = (int*)calloc(2,sizeof(int));
int answer1 = 0;
int answer2 = 0;
int hash1[101] = {0};
int hash2[101] = {0};
//记录数组1出现的元素
for(int i = 0;i<nums1Size;i++)
{
hash1[nums1[i]] = 1;
}
//记录数组2出现的元素
for(int i = 0;i<nums2Size;i++)
{
hash2[nums2[i]] = 1;
}
//遍历数组1,每次判断当前元素是否在数组2中
for(int i = 0;i<nums2Size;i++)
{
if(hash1[nums2[i]])
ans[1]+=1;
}
//遍历数组2,每次判断当前元素是否在数组1中
for(int i = 0;i<nums1Size;i++)
{
if(hash2[nums1[i]])
ans[0]+=1;
}
*returnSize = 2;
return ans;
}