原题链接: 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;
}

最新文章

  1. 数学思想:为何我们把 x&#178;读作x平方
  2. ArcGisEngineForJava开发
  3. 第一个ruby程序
  4. ELF Format 笔记(九)—— Elf32_Sym 结构的 st_value 和 st_shndx 成员
  5. hdu 1166
  6. C#学校班级自动升级实现代码
  7. [问题2014S12] 复旦高等代数II(13级)每周一题(第十二教学周)
  8. OC测试错误整理
  9. jquery格式化时间戳 2011-01-01
  10. 【Hadoop需要的Jar包】Hadoop编程-pom.xml文件
  11. JDBC 事务控制
  12. 文件上传~Uploadify上传控件
  13. UIButton set touch handler in code
  14. Linux下的bc计算器
  15. 一个php开发的用于路由器的小功能
  16. windows Apache 环境下配置支持HTTPS的SSL证书
  17. mysql查询优化之三:查询优化器提示(hint)
  18. Spring Import注解
  19. iOS7 UIKit动力学-重力特性UIGravityBehavior
  20. XSS攻击处理方案

热门文章

  1. MyBatis(1)-- MyBatis介绍
  2. Unity混合天空盒
  3. video3
  4. SpringBoot整合MybatisPlus3.X之逻辑删除(三)
  5. Spring Cloud alibaba网关 sentinel zuul 四 限流熔断
  6. JVM内存结构、参数调优和内存泄露分析
  7. Pycharm 专业版激活码(转) 有效期到2020/06月
  8. 图片瀑布流,so easy!
  9. 强大的CompletableFuture
  10. [考试反思]1002csp-s模拟测试57:平庸