今天是个WA的好日子啊~~

P2010 [NOIP2016 普及组] 回文日期

hexuchen @ 2023-06-13 17:43:51

求助!为啥只得了50分?有TLE也有WA QWQ

戳这里

#include <bits/stdc++.h>
using namespace std; 
int y[13]={0,32,29,32,31,32,31,32,32,31,32,31,32},sum=0;
bool pan(int a){
    int a2,wei=0,b,c;
    a2=a;
    while(a2!=0){
        wei++;
        a2/=10;
    }
    c=pow(10,wei-1);
    a2=a; 
    for(int i=1;i<=wei;i++){
        b+=c*(a2%10);
        c/=10;
        a2/=10;
    }
    if(b==a)
        return true;
    else
        return false; 
}
int main(){
    int date1,date2,year,mouth,day,year2,mouth2,day2;
    cin>>date1>>date2;
    day=date1%100;
    mouth=date1/100%100;
    year=date1/10000;
    day2=date2%100;
    mouth2=date2/100%100;
    year2=date2/10000;
    while(!(year==year2 && mouth==mouth2 && day==day2)){
        day++;
        if(day==y[mouth]){
            if(mouth!=2)
                day=1;
            else
                if((year%4!=0 || year%10==0) && year%400!=0)
                    day=1;
                else{
                    if(pan(year*10000+mouth*100+day))
                        sum++;
                    continue;
                }
            mouth++;
            if(mouth==13){
                mouth=1;
                year++;
            }
        }
        if(pan(year*10000+mouth*100+day))
            sum++;
    }
    cout<<sum;
    return 0;
}

有没有大佬来九九我


by Karieciation @ 2023-06-13 20:18:43

你这里思路有误


by Karieciation @ 2023-06-13 20:19:12

我给你放一段我的AC代码


by Karieciation @ 2023-06-13 20:22:10

# include <cstdio>
const int d[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int s, b, n, cnt;
int a[400];
void work() {
    for(int k = 0; k < 12; k++)
        for(int i = 0; i <= d[k]; i++)
            if (i % 10)
                a[++n] = k * 100 + 100 + i + i % 10 * 10000000 + i / 10 * 1000000 + (k + 1) % 10 * 100000 + (k + 1) / 10 * 10000;
}
signed main() {
    work();
    scanf("%d%d", &s, &b);
    for(int i = 1; i <= n; i++)
        if (a[i] <= b && a[i] >= s)
            cnt++;
    printf("%d\n", cnt);
    return 0;
}

by Karieciation @ 2023-06-13 20:26:53

这道题的思路应该是枚举一年中的天数,将所有可能的回文日期提前记录好,因为一年只有365天(闰年366天),回文日期的数量不超过366,时间复杂度与空间复杂度都允许,最后判断哪些回文日期在起始日期与终止日期之间即可。


by hexuchen @ 2023-06-14 17:03:29

@MZjtW 知道了,谢谢大神指导!


|