60分求调

P2010 [NOIP2016 普及组] 回文日期

1313113lmx @ 2023-12-29 20:57:22

#include <bits/stdc++.h>
using namespace std;
const int MAXN=1e6+5,MAXB=2e7;
#define ll long long
int d1,d2,ans;
int getday(int y,int m) {
    if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
        return 31;
    if(m==4||m==6||m==9||m==11)
        return 30;
    if(y%4==0&&y%100!=0||y%400==0)
        return 29;
    else
        return 28;
}
int nextday(int x) {
    int y=x/10000;
    int m=x/100%100;
    int d=x%100;
    if(d==getday(y,m))
        d+=1;
    else if(m!=12) {
        m+=1;
        d=1;
    } else {
        y+=1;
        m=1;
        d=1;
    }
    return 10000*y+100*m+d;
}
int rev(int x) {
    int ans=0;
    while(x) {
        ans=ans*10+(x%10);
        x/=10;
    }
    return ans;
}
int main() {
    scanf("%d%d",&d1,&d2);
    for(int d=d1; d<=d2; d=nextday(d))
        if(d==rev(d))
            ans++;
    printf("%d",ans);
    return 0;
}

by Pitiless_boy @ 2023-12-29 21:25:17


    if((y%4==0&&y%100!=0)||y%400==0)
        return 29;
    else
        return 28;

by Pitiless_boy @ 2023-12-29 21:25:28

@1313113lmx


by 0oOo0 @ 2023-12-29 21:29:55

@1313113lmx

以后记得要相互判断的布尔值用括号括起来。


by Pitiless_boy @ 2023-12-29 21:35:30

@1313113lmx 可以改成

#include<bits/stdc++.h>
using namespace std;
int x1, x2, ans;
int rev(int x){
    int y = 0;
    while(x){
        y = y * 10 + (x % 10);
        x /= 10;
    }
    return y;
}
int getday(int x, int y){
    if(y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12)
        return 31;
    if(y == 2 || y == 4 || y == 6 || y == 9 || y == 11)
        return 30;
    if((x % 4 == 0 && x % 100 != 0) || (x % 400 == 0))
        return 29;
    else
        return 28;      
}
int main() {
    scanf("%d%d", &x1, &x2);
    for(int y = 1000; y <= 9999; y++){
        int revy = rev(y);
        int m = revy / 100 % 100;
        int d = revy % 100;
        if(m >= 1 && m <= 12 && d >= 1 && d <= getday(y, m)){
            int x = y * 10000 + m * 100 + d;
            if(x >= x1 && x <= x2)
                ans++;
        }
    }
    printf("%d", ans);
}

by youzhixin @ 2024-02-01 15:53:20

1


by Godric__Gryffindor @ 2024-02-05 20:58:44

&& 比 ||的优先级高,问题不在这。


|