帮帮孩子c语言70分,8,9,10WA,求8的所有会问日期

P2010 [NOIP2016 普及组] 回文日期

jango @ 2024-02-20 13:16:46

好心人帮帮孩子

#include<stdio.h>

int fun_huiwen(int dt)
{
    int rt,lf;
    int out=1;
    for(int i=10;i<=10000;i*=10)
    {
        rt=(dt*10/i)%10;
        lf=(dt/(100000000/i))%10;
        if(rt!=lf)
        {
            out=0;
            break;
        }
    }
    return out;
}
int fun_date(int dt)
{
    int y,m,d,rn=0;
    y=dt/10000;
    m=(dt/100)%100;
    d=dt%100;
    if(y%400==0)
    {
        rn=1;
    }
    else if(y%100!=0&&y%4==0)
    {
        rn=1;
    }
    switch(m)
    {
        case 1:if(d==32){d=1;m+=1;}
        case 2:
        if(rn)
        {   if(d==30){d=1;m+=1;}}
        else if(rn==0){if(d==29){d=1;m+=1;}}
        case 3:if(d==32){d=1;m+=1;}
        case 4:if(d==31){d=1;m+=1;}
        case 5:if(d==32){d=1;m+=1;}
        case 6:if(d==31){d=1;m+=1;}
        case 7:if(d==32){d=1;m+=1;}
        case 8:if(d==32){d=1;m+=1;}
        case 9:if(d==31){d=1;m+=1;}
        case 10:if(d==32){d=1;m+=1;}
        case 11:if(d==31){d=1;m+=1;}
        case 12:if(d==32){d=1;m+=1;}
    }
    if(m==13)
    {
        m=1;
        y+=1;
    }
    dt=10000*y+100*m+d;
    return dt;
}
int main()
{
    int d1,d2,n=0;
    scanf("%d",&d1);
    scanf("%d",&d2);
    for(int dt=d1;dt<=d2;)
    {
        n+=fun_huiwen(dt);
        if(fun_huiwen(dt))
        {
            printf("%d\n",dt);
        }
        dt++;
        dt=fun_date(dt);
    }
    printf("%d",n);
    return 0;
}

by zcxxcz2110 @ 2024-02-21 13:59:00

@jango t<r为啥不能进if


by zcxxcz2110 @ 2024-02-21 14:02:36

@jango C语言小于号不能进if吗(主要是我学的是C++,感觉和C差不多,就给你发了),而且我给你发的C语言的代码能过啊


by zcxxcz2110 @ 2024-02-21 14:20:16

#include <stdio.h>
const int D[15] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//1~12月的天数

int main()
{
    int l, r, ans = 0, t;
    scanf("%d %d", &l, &r);//输入
    for(int i = 1; i <= 12; i++)//枚举每月
    {
        for(int j = 1; j <= D[i]; j++)//枚举每天
        {
            t = j % 10 * 10000000 + j / 10 * 1000000 + i % 10 * 100000 + i / 10 * 10000 + i * 100 + j;//构造回文日期(j的倒序乘100+i的倒序年i月j日也就是上面的算式)
            if(t >= l && t <= r)//判断是不是在范围内
                ++ans;//回文日期的个数+1
        }
    }
    printf("%d", ans);//输出
    return 0;
}

by zcxxcz2110 @ 2024-02-21 14:21:24

@jango 这是我写的解析,这样应该能懂了吧


by jango @ 2024-02-21 23:25:27

@zcxxcz2110 大佬牛,代码好简洁,根据你代码跑的结果debug发现我之前那的switch写错了,后来改了改。```c

include<stdio.h>

int fun_huiwen(int dt) { int rt,lf; int out=1; for(int i=10;i<=10000;i=10) { rt=(dt10/i)%10; lf=(dt/(100000000/i))%10; if(rt!=lf) { out=0; break; } } return out; } int fun_date(int dt) { int y,m,d,rn=0; int mouth[12]={31,28,31,30,31,30,31,31,30,31,30,31}; y=dt/10000; m=(dt/100)%100; d=dt%100; if(y%400==0) { rn=1; } else if(y%100!=0&&y%4==0) { rn=1; } if(rn) { mouth[1]=29; } else { mouth[1]=28; } if(d>mouth[m-1]) { d=1; m+=1; } if(m==13) { m=1; y+=1; } dt=10000y+100m+d; return dt; } int main() { int d1,d2,n=0; scanf("%d",&d1); scanf("%d",&d2); for(int dt=d1;dt<=d2;) { n+=fun_huiwen(dt); dt++; dt=fun_date(dt); } printf("%d",n); return 0; }


by jango @ 2024-02-21 23:26:17

#include<stdio.h>

int fun_huiwen(int dt)
{
    int rt,lf;
    int out=1;
    for(int i=10;i<=10000;i*=10)
    {
        rt=(dt*10/i)%10;
        lf=(dt/(100000000/i))%10;
        if(rt!=lf)
        {
            out=0;
            break;
        }
    }
    return out;
}
int fun_date(int dt)
{
    int y,m,d,rn=0;
    int mouth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    y=dt/10000;
    m=(dt/100)%100;
    d=dt%100;
    if(y%400==0)
    {
        rn=1;
    }
    else if(y%100!=0&&y%4==0)
    {
        rn=1;
    }
    if(rn)
    {
        mouth[1]=29;
    }
    else
    {
        mouth[1]=28;
    }
    if(d>mouth[m-1])
    {
        d=1;
        m+=1;
    }
    if(m==13)
    {
        m=1;
        y+=1;
    }
    dt=10000*y+100*m+d;
    return dt;
}
int main()
{
    int d1,d2,n=0;
    scanf("%d",&d1);
    scanf("%d",&d2);
    for(int dt=d1;dt<=d2;)
    {
        n+=fun_huiwen(dt);

        dt++;
        dt=fun_date(dt);
    }
    printf("%d",n);
    return 0;
}

@jango


by zcxxcz2110 @ 2024-02-22 11:44:17

点个关注呗,谢谢


上一页 |