zoukankan      html  css  js  c++  java
  • 题解-洛谷P2010-回文日期

    原题链接: https://www.luogu.org/problem/P2010

    题目简述:

    牛牛习惯用8位数字表示一个日期,其中,前4位代表年份,接下来2位代表月份,最后22位代表日期。显然:一个日期只有一种表示方法,而两个不同的日期的表 示方法不会相同。

    牛牛认为,一个日期是回文的,当且仅当表示这个日期的8位数字是回文的。现 在,牛牛想知道:在他指定的两个日期之间包含这两个日期本身),有多少个真实存 在的日期是回文的。

    一个8位数字是回文的,当且仅当对于所有的i(1i8)i ( 1 le i le 8)从左向右数的第i个 数字和第9i9-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;
    }
    
  • 相关阅读:
    React开发入门
    API爬虫--Twitter实战
    网页爬虫--scrapy入门
    爬虫入门(实用向)
    随谈10年的技术生涯和技术成长
    html元素的显示和隐藏
    Ubuntu下用cue文件对ape和wav文件自动分轨
    Bash内置命令exec和重定向
    Bash提示符
    Bash启动选项
  • 原文地址:https://www.cnblogs.com/littlefrog/p/11939498.html
Copyright © 2011-2022 走看看