为毛TLE一个点

P2010 [NOIP2016 普及组] 回文日期

qss_ @ 2019-08-10 21:48:28

请dalao指教


#include <ctime>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
bool ly(int x){
    if (x%4==0&&x%100!=0||x%400==0) return 1;
    return 0;
}
bool id(int x){
    int d=x%100;
    int m=(x%10000-x%100)/100; 
    int y=(x-100*m-d)/10000;
    int md;
    switch (m){
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            md=31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            md=30;
            break;
        case 2:
            if (ly(y)==1) md=29;
            if (ly(y)==0) md=28;
            break;
    }
    if (d>md) return 0;
    if (m>12||m<1) return 0;
    return 1;
}
int main(){
    int n,m;
    cin>>n>>m;
    int s=0;
    for (int i=n;i<=m;i++){
        if (id(i)==0) continue;
        int t=i;
        int d[9]={0};
        for (int j=8;j>=1;j--){
            d[j]=t%10;
            t/=10;
        }
        if (d[1]==d[8]&&d[2]==d[7]&&d[3]==d[6]&&d[4]==d[5]) s++;
    }
    cout<<s;
    return 0;
}

by    吾皇 @ 2019-08-10 21:54:26

从n枚举到m..肯定炸掉啊


by _zby_ @ 2019-08-10 21:59:25

https://www.luogu.org/paste/o1ewhpzh


by guoxinyugz @ 2019-08-10 22:02:21

哪能这样枚举呢……

建议考虑构造date1到date2之间的回文日期


by yubing_lml @ 2019-08-30 11:18:04

我就是枚举的呀,通过了~

#include<iostream>
using namespace std;

int date1, date2, ans = 0;

bool isLeap(int year)
{
    if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
        return true;
    return false;
}

bool isReverse(int n)
{
    int i = n, num = 0;
    while (i)
    {
        num = num * 10 + i % 10;
        i /= 10;
    }
    if (num == n)
        return true;
    else
        return false;
}

int next(int date)
{
    int year = date / 10000;
    int month = (date % 10000) / 100;
    int day = date % 100;
    bool change_month = false;
    if (month == 4 || month == 6 || month == 9 || month == 11)
    {
        if (day == 30)
            change_month = true;
    }
    else if (month == 2)
    {
        if ((isLeap(year) && day == 29) || (!isLeap(year) && day == 28))
            change_month = true;
    }
    else
    {
        if (day == 31)
            change_month = true;
    }
    if (change_month == true)
    {
        month++;
        day = 1;
    }
    else
        day++;
    if (month > 12)
    {
        year++;
        month = 1;
    }
    return year * 10000 + month * 100 + day;
}

int main()
{
    cout.sync_with_stdio(false);
    cin >> date1 >> date2;
    for (int i = date1; i <= date2; i=next(i))
        if (isReverse(i))
            ans++;
    cout << ans;
    cin >> date1;
    return 0;
}

by cs613 @ 2019-11-16 11:18:50

枚举年份就行了```cpp

include<cstdio>

//#include<cstring> //#include<algorithm> //#include<cmath> using namespace std; int daylist[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; inline void in(int &x){ x=0;register char c=getchar(); while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9') x=x10+c-'0',c=getchar(); } int main(){ int data1,data2; in(data1);in(data2); int year1=data1/10000,year2=data2/10000,ans=0; // printf("%d %d\n",year1,year2); while(year1<=year2){ int dm=year1%101000+year1/10%10100+year1/100%1010+year1/1000; // printf("%d\n",dm); int month=dm/100,day=dm%100; // printf("%d %d\n",month,day); if(year1%4==0&&year1%100||year1%400==0) daylist[2]++; if(month>0&&day>0&&month<=12&&day<=daylist[month]) ans++; if(year1%4==0&&year1%100||year1%400==0) daylist[2]--; year1++; } printf("%d",ans); return 0; }


by cs613 @ 2019-11-16 11:19:42

我压行的


|