日期類
#pragma once#include <iostream>
#include <assert.h>
using namespace std;class Date
{
public:// 構造會頻繁調用,所以直接放在類里面(類里面的成員函數默認為內聯)Date(int year = 1, int month = 1, int day = 1)//構造{_year = year;_month = month;_day = day;//if (!CheckDate())//{// Print();// cout << "剛構造的日期非法" << endl;//}assert(CheckDate());}void Print() const; // 打印int GetMonthDay(int year, int month)// 獲取某年某月的天數{static int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int day = days[month];if (month == 2&& ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))){day += 1;}return day;}bool CheckDate()// 檢查日期是否合法{if (_year >= 1&& _month > 0 && _month < 13&& _day > 0 && _day <= GetMonthDay(_year, _month)){return true;}else{return false;}}bool operator==(const Date& d) const;bool operator>(const Date& d) const;bool operator!=(const Date& d) const;bool operator>=(const Date& d) const;bool operator<(const Date& d) const;bool operator<=(const Date& d) const;Date& operator+=(int day);Date operator+(int day) const;Date& operator-=(int day);Date operator-(int day) const;// 特殊處理,使用重載區分,后置++重載增加一個int參數跟前置構成函數重載進行區分Date& operator++(); // 前置Date operator++(int); // 后置Date& operator--();// 前置Date operator--(int);// 后置int operator-(const Date& d) const; //日期減日期void PrintWeekDay() const; //返回*this是星期幾private:int _year;int _month;int _day;
};#include "Date.h"// void Date::Print(const Date* const this)
void Date::Print() const
{cout << _year << "年" << _month << "月" << _day << "日" << endl;
}// 任何一個類,只需要寫一個> == 或者 < ==重載 剩下比較運算符重載復用即可
bool Date::operator== (const Date& d) const
{return _year == d._year&& _month == d._month&& _day == d._day;
}bool Date::operator>(const Date& d) const
{if ((_year > d._year)|| (_year == d._year && _month > d._month)|| (_year == d._year && _month == d._month && _day > d._day)){return true;}else{return false;}
}bool Date::operator!=(const Date& d) const
{return !(*this == d);
}bool Date::operator>=(const Date& d) const
{return (*this > d) || (*this == d);
}bool Date::operator<(const Date& d) const
{return !(*this >= d);
}bool Date::operator<=(const Date& d) const
{return !(*this > d);
}Date& Date::operator+=(int day)
{if (day < 0){return *this -= -day;}_day += day;while (_day > GetMonthDay(_year, _month)){_day -= GetMonthDay(_year, _month);_month++;if (_month == 13){_year++;_month = 1;}}return *this;
}Date Date::operator+(int day) const
{Date ret = *this; ret += day;return ret;// 出了作用域ret對象就不在了,所以不能用引用返回
}Date& Date::operator-=(int day)
{if (day < 0){return *this += -day;}_day -= day;while (_day <= 0){_month--;if (_month == 0){_year--;_month = 12;}_day += GetMonthDay(_year, _month);}return *this;
}Date Date::operator-(int day) const
{Date ret = *this;ret -= day;// ret.operator-=(day);return ret;// 和 + 一樣,出了作用域ret對象就不在了,所以不能用引用返回
}Date& Date::operator++() // 前置
{return *this += 1;
}
Date Date::operator++(int) // 后置
{Date ret = *this;*this += 1;return ret;
}Date& Date::operator--() // 前置
{return *this -= 1;
}
Date Date::operator--(int) // 后置
{Date ret = *this;*this -= 1;return ret;
}int Date::operator-(const Date& d) const
{int ret = 0;int flag = -1;Date min = *this;//默認第一個小,返回的時候乘上 -1Date max = d;if (*this > d)//默認錯誤,把小和大重置,返回時乘上 1{flag = 1;min = d;max = *this;}while (min != max){++min;++ret;}return ret * flag;
}void Date::PrintWeekDay() const //打印*this是星期幾
{const char* Week[] = { "星期一","星期二" ,"星期三" , "星期四" ,"星期五" , "星期六" , "星期天" };Date flag(1900, 1, 1); //1900年1月1日是星期一,自己減自己為0,對應下標0cout << Week[(*this - flag) % 7] << endl;
}#include "Date.h"void TestDate7()
{Date d1(2023, 5, 5);Date d2(2023, 6, 7);d1.Print();d1.PrintWeekDay();d2.Print();d2.PrintWeekDay();Date d3(1900, 1, 7);Date d4(2050, 6, 7);d3.Print();d3.PrintWeekDay();d4.Print();d4.PrintWeekDay();
}int main()
{TestDate7();return 0;
}
485. 最大連續 1 的個數 - 力扣(LeetCode)
劍指 Offer 04. 二維數組中的查找11111111
二維數組中的查找_牛客題霸_牛客網 (nowcoder.com)
劍指 Offer 11. 旋轉數組的最小數字11111111
旋轉數組的最小數字_牛客題霸_牛客網 (nowcoder.com)
劍指 Offer 21. 調整數組順序使奇數位于偶數前面111111111
調整數組順序使奇數位于偶數前面__牛客網 (nowcoder.com)
劍指 Offer 39. 數組中出現次數超過一半的數字11111111111
數組中出現次數超過一半的數字_牛客題霸_牛客網 (nowcoder.com)
劍指 Offer 05. 替換空格111111111
替換空格_牛客題霸_牛客網 (nowcoder.com)
劍指 Offer 06. 從尾到頭打印鏈表
從尾到頭打印鏈表_牛客題霸_牛客網 (nowcoder.com)
劍指 Offer 07. 重建二叉樹
重建二叉樹_牛客題霸_牛客網 (nowcoder.com)
動態規劃:
劍指 Offer 42. 連續子數組的最大和111111111111111
連續子數組的最大和_牛客題霸_牛客網 (nowcoder.com)
hash
排序算法的特殊理解
劍指 Offer 52. 兩個鏈表的第一個公共節點
兩個鏈表的第一個公共結點_牛客題霸_牛客網 (nowcoder.com)
7 算法公開課
動態規劃
劍指 Offer 10- I. 斐波那契數列
劍指 Offer II 098. 路徑的數目11111111111
劍指 Offer II 099. 最小路徑之和
背包問題
背包問題_嗶哩嗶哩筆試題_牛客網 (nowcoder.com)
劍指 Offer II 094. 最少回文分割
劍指 Offer II 086. 分割回文子字符串
72. 編輯距離
115. 不同的子序列
貪心算法
栗子:選擇排序
1221. 分割平衡字符串111111111111111
122. 買賣股票的最佳時機 II1111111111111
55. 跳躍游戲11111111111
435. 無重疊區間1111111000000
回溯算法
深度優先 : DFS
栗子 : 排列組合
690. 員工的重要性
733. 圖像渲染
733相似:463. 島嶼的周長
130. 被圍繞的區域
130相似:劍指 Offer II 105. 島嶼的最大面積
17. 電話號碼的字母組合11111111
17相似:401. 二進制手表
39. 組合總和
1079. 活字印刷
51. N 皇后
51相似:52. N 皇后 II
廣度優先: BFS
栗子:迷宮問題