前言
在做题中遇到了几次的日期问题之后,与大家分享一下相关日期问题常用的模板,如果喜欢可以点个赞!
模板
#include <iostream>
using namespace std;
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 判断日期的合法性
bool check_valid(int date) //形如20210305
{
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (month <= 0 || month >= 13) return false;
if (day == 0 || month != 2 && day > months[month]) return false;
if (month == 2)
{
int leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
if (day > 28 + leap) return false;
}
return true;
}
// 得到某年某月的天数
int get(int year, int month)
{
if (month != 2) return months[month];
else
{
// 2月
int leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
return 28 + leap;
}
}
// 判断两个日期之间有多少个回文日期
int get(int date1, int date2, int k) //k使函数签名不同从而编译通过
{
int ans = 0;
for (int i = 1000; i < 10000; i++)
{
int date = i, x = i;
for (int j = 0; j < 4; j++) date = date * 10 + x % 10, x /= 10; //根据年份构造出回文日期
if (date1 <= date && date <= date2 && check_valid(date)) ans++;
}
return ans;
}
// 给定年月日,经过n天后对应的日期
void pass(int y, int m, int d, int n)
{
while (n--)
{
d++;
if (d > get(y, m)) m++, d = 1;
if (m > 12) y++, m = 1;
}
printf("%d-%02d-%02d\n", y, m, d);
}
int main()
{
return 0;
}
参考练习题(附上代码)
466. 回文日期:https://www.acwing.com/problem/content/468/
#include <iostream>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check_valid(int date) //1234 5678
{
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (month <= 0 || month >= 13) return false;
if (day == 0 || month != 2 && day > days[month]) return false;
if (month == 2)
{
int leap = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
if (day > 28 + leap) return false;
}
return true;
}
int main()
{
int date1, date2;
cin >> date1 >> date2;
int ans = 0;
for (int i = 1000; i < 10000; i++)
{
int date = i, x = i;
for (int j = 0; j < 4; j++) date = date * 10 + x % 10, x /= 10;
if (date1 <= date && date <= date2 && check_valid(date)) ans++;
}
cout << ans << endl;
return 0;
}
1229. 日期问题:https://www.acwing.com/problem/content/1231/
#include <iostream>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check_valid(int year, int month, int day)
{
if (month <= 0 || month >= 13) return false;
if (day == 0 || month != 2 && day > days[month]) return false;
if (month == 2)
{
int leap = (year % 400 == 0 || year % 4 == 0 && year % 100 != 0);
if (day > 28 + leap) return false;
}
return true;
}
int main()
{
int a, b, c;
scanf("%d/%d/%d", &a, &b, &c);
for (int i = 19600101; i <= 20591231; i++)
{
int year = i / 10000, month = i % 10000 / 100, day = i % 100;
if (check_valid(year, month, day))
{
if (year % 100 == a && month == b && day == c ||
month == a && day == b && year % 100 == c ||
day == a && month == b && year % 100 == c)
printf("%d-%02d-%02d\n", year, month, day);
}
}
return 0;
}
2867. 回文日期:https://www.acwing.com/problem/content/2870/
#include <iostream>
using namespace std;
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 判断日期的合法性
bool check_valid(int date) //形如20210305
{
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (month <= 0 || month >= 13) return false;
if (day == 0 || month != 2 && day > months[month]) return false;
if (month == 2)
{
int leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
if (day > 28 + leap) return false;
}
return true;
}
// 判断date是否满足ABABBABA形式
bool check(int date)
{
int year = date / 10000;
int month = date % 10000 / 100;
int day = date % 100;
if (month % 10 == day / 10) return false; //A==B return false
if ((month % 10 == day % 10) && (month / 10 == day / 10)) return true;
return false;
}
bool st1, st2;
int ans1, ans2;
int main()
{
int date;
cin >> date;
int year = date / 10000;
while (true)
{
int new_date = year, x = year;
for (int i = 0; i < 4; i++) new_date = new_date * 10 + x % 10, x /= 10; //构造回文日期
if (new_date == date) //构造出来的是同一天
{
year++;
continue;
}
if (check_valid(new_date))
{
if (!st1) st1 = true, ans1 = new_date;
if (!st2 && check(new_date)) st2 = true, ans2 = new_date;
}
year ++;
if (st1 && st2) break; //找到两个解则退出
}
printf("%d\n%d", ans1, ans2);
return 0;
}
3218. 日期计算:https://www.acwing.com/problem/content/3221/
#include <iostream>
using namespace std;
int months[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// 得到某年某月的天数
int get(int year, int month)
{
if (month != 2) return months[month];
else
{
// 2月
int leap = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0);
return 28 + leap;
}
}
// 给定年月日,经过n天后对应的日期
void pass(int y, int m, int d, int n)
{
while (n--)
{
d++;
if (d > get(y, m)) m++, d = 1;
if (m > 12) y++, m = 1;
}
printf("%d\n%d\n", m, d);
}
int main()
{
int y, n;
cin >> y >> n;
int m = 1, d = 0;
pass(y, 1, d, n);
return 0;
}
感谢大佬!
666666
感谢大佬
收藏了 感谢大佬
有用,点赞
收藏了 感谢大佬
感谢大佬
收藏了,帮大忙了
8错8错
写得很不错,收藏了