题解-洛谷P2010-回文日期
2024-09-01 17:16:15
原题链接: https://www.luogu.org/problem/P2010
题目简述:
牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后22位代表日期。显然:一个日期只有一种表示方法,而两个不同的日期的表 示方法不会相同。
牛牛认为,一个日期是回文的,当且仅当表示这个日期的8位数字是回文的。现 在,牛牛想知道:在他指定的两个日期之间包含这两个日期本身),有多少个真实存 在的日期是回文的。
一个8位数字是回文的,当且仅当对于所有的i(1≤i≤8)i ( 1 \le i \le 8)i(1≤i≤8)从左向右数的第i个 数字和第9−i9-i9−i个数字(即从右向左数的第i个数字)是相同的。
例如:
对于2016年11月19日,用88位数字20161119表示,它不是回文的。
对于2010年1月2日,用88位数字20100102表示,它是回文的。
对于2010年10月2日,用88位数字20101002表示,它不是回文的。
算出两个日期之间所有的回文日期。
思路:
- 回文日期要是真实存在的
(这还用我说吗??) - 最后一组是极限数据,大概像这样:
10010101
99991231
循环枚举肯定会超时
- 所以需要通过枚举日期来”制造回文“,类似与这样:
1999
年中的回文日期应该是: 19999991
虽然它是不存在的
2010
年中的回文日期应该是: 20100102
代码:
#include <bits/stdc++.h>
using namespace std;
int a,b,c,d;
int e,f;
bool runnian(int year) {
if(year%100==0) {
if(year%400==0) {
return 1;
}
return 0;
}//千禧年特判
else {
if(year%4==0) {
return 1;
}
return 0;
}
}
bool judge(int n) {
if(n==1||n==3||n==5||n==7||n==8||n==10||n==12) {
return 1;
}
return 0;
}
bool in(int year,int month,int day) {
if(year>d||year<a) {
return 0;
}
//——————————————————
if(year==a&&month<b) {
return 0;
}
if(year==a&&day>c&&month==b) {
return 0;
}
if(month==0||day==0||year==0) {
return 0;
}
//————————————————————-
if(year==d&&month>e) {
return 0;
}
if(year==d&&day>f&&month==e) {
return 0;
}
if(month>=13) {
return 0;
}
if(day>=32) {
return 0;
}
if(month==2) {
if(day>28&&(!runnian(year))) {
return 0;
}
if(day>29&&(runnian(year))) {
return 0;
}
return 1;
} else {
if(day>31&&judge(month)) {
return 0;
}
if(day>30&&!judge(month)) {
return 0;
}
return 1;
}
}
int main() {
string n,m;
cin>>n>>m;
a = atoi(n.substr(0,4).c_str());
b = atoi(n.substr(4,2).c_str());
c = atoi(n.substr(6,2).c_str());
d = atoi(m.substr(0,4).c_str());
e = atoi(m.substr(4,2).c_str());
f = atoi(m.substr(6,2).c_str());
int year = a;
int month = b;
int day = c;
int ans = 0;
for(int i = year;i<=d;++i) {
int o,p,q;
o = year;
string a;
stringstream ss;
ss<<year;
ss>>a;
char b[101010];
int len = 0;
for(int i = a.length()-1;i>=0;--i) {
b[len] = a[i];
len++;
}
string c(b);
p = atoi(c.substr(0,2).c_str());
q = atoi(c.substr(2,2).c_str());
year++;
if(in(o,p,q)) {
ans++;
}
}
cout<<ans<<endl;
}
最新文章
- 数学思想:为何我们把 x&#178;读作x平方
- ArcGisEngineForJava开发
- 第一个ruby程序
- ELF Format 笔记(九)—— Elf32_Sym 结构的 st_value 和 st_shndx 成员
- hdu 1166
- C#学校班级自动升级实现代码
- [问题2014S12] 复旦高等代数II(13级)每周一题(第十二教学周)
- OC测试错误整理
- jquery格式化时间戳 2011-01-01
- 【Hadoop需要的Jar包】Hadoop编程-pom.xml文件
- JDBC 事务控制
- 文件上传~Uploadify上传控件
- UIButton set touch handler in code
- Linux下的bc计算器
- 一个php开发的用于路由器的小功能
- windows Apache 环境下配置支持HTTPS的SSL证书
- mysql查询优化之三:查询优化器提示(hint)
- Spring Import注解
- iOS7 UIKit动力学-重力特性UIGravityBehavior
- XSS攻击处理方案