70分求助!!help

P2010 [NOIP2016 普及组] 回文日期

Exile_Code @ 2023-07-21 21:19:39

#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <bitset>

typedef long long ll;
#define first x
#define second y

int main() {

    int y, m, d, yy, mm, dd;
    scanf("%4d%2d%2d", &y, &m, &d);
    scanf("%4d%2d%2d", &yy, &mm, &dd);

    int mouth[13] = { 0,31,28,31,30,31
                                   ,30,31,31,30,31,30,31 };

    long long res = 0;

    for (int i = y; i <= yy; i++) {
        mouth[2] = 28;

        if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
            mouth[2] = 29;

        string a = to_string(i);
        reverse(a.begin(), a.end());//年份反转
        int a_m = ((a[0] - '0') * 10 + a[1] - '0');
        int a_n  = ((a[2] - '0') * 10 + a[3] - '0');//获取月日

        if (y==yy){//开始和结尾相等的时候
            if ( a_m >= m && a_n >= d
            &&a_m<=mm&&a_m<=dd
            &&a_m<= 12 && a_m >= 1 
            &&a_n <= mouth[a_m] && a_n > 0)
                res++;
        }
        else if (i == y) {//开始的时候
            if (a_m >= m && a_n >= d 
                && a_m <= 12 && a_m >= 1
                && a_n <= mouth[a_m] && a_n > 0)
                res++;
        }
        else if (i == yy) {//结尾的时候
            if (a_m <= mm && a_m <= dd
                && a_m <= 12 && a_m >= 1
                && a_n <= mouth[a_m]&&a_n>0)
                res++;
        }
        else//其余情况
            if (a_m<=12&&a_m>=1
                &&a_n <= mouth[a_m] && a_n > 0)
                res++;
    }

    cout << res ;

    return 0;
}

by Exile_Code @ 2023-07-21 21:26:17

第一个测试用例明明过了啊,为什么它也wa了啊,绷不住了


by Lizj @ 2023-07-21 21:27:50

@Exile_Code 你判断了不合法年份吗?


by Exile_Code @ 2023-07-21 21:37:18

@Mr__AC 判断了啊,在反转之后判断的,大佬求助啊


by Lizj @ 2023-07-21 21:39:53

@Exile_Code 你把i==y,i==yy和y==yy的情况放在循环内会造成重复判断,稍微改动即可。

代码:

//test
#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <string>
#include <cmath>
#include <bitset>

typedef long long ll;
#define first x
#define second y

int main() {

    int y, m, d, yy, mm, dd;
    scanf("%4d%2d%2d", &y, &m, &d);
    scanf("%4d%2d%2d", &yy, &mm, &dd);

    int mouth[13] = { 0,31,28,31,30,31
                                   ,30,31,31,30,31,30,31 };

    long long res = 0;
    if (y==yy&&m==mm&&d==dd){//开始和结尾相等的时候
        int y1=d%10*1000+d/10%10*100+m%10*10+m/10%10;
        if(y1==y){
            res++;
        }
        cout<<res;
        return 0;
    }
    for (int i = y; i <= yy; i++) {
        mouth[2] = 28;

        if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)
            mouth[2] = 29;

        string a = to_string(i);
        reverse(a.begin(), a.end());//年份反转
        int a_m = ((a[0] - '0') * 10 + a[1] - '0');
        int a_n  = ((a[2] - '0') * 10 + a[3] - '0');//获取月日

        if (y==yy){//开始和结尾相等的时候
            if ( a_m >= m && a_n >= d
            &&a_m<=mm&&a_m<=dd
            &&a_m<= 12 && a_m >= 1 
            &&a_n <= mouth[a_m] && a_n > 0)
                res++;
        }

        else//其余情况
            if (a_m<=12&&a_m>=1
                &&a_n <= mouth[a_m] && a_n > 0)
                res++;
    }

    cout << res ;

    return 0;
}

求关注


by Lizj @ 2023-07-21 21:42:17

@Exile_Code 只需要删去i==y,i==yy的情况,把y==yy的情况提出循环判断即可。

如果对你有用,不妨关注我


by Lizj @ 2023-07-21 22:18:47

@Exile_Code 看到了麻烦关注一下


by Exile_Code @ 2023-07-22 07:34:53

@Mr__AC 关注了已经,但是为什么重复判断,答案会错误啊,else if不是只判断一次嘛?


by Exile_Code @ 2023-07-22 07:35:21

@Mr__AC 谢谢大佬


by Exile_Code @ 2023-07-22 07:38:52

@Mr__AC 他是在哪里重复了啊?能否指点一下


by Lizj @ 2023-07-22 07:59:47

@Exile_Code 把判断放在里面每次循环都会判断一次,所以会重复加


| 下一页