PTA 7-5 6004 18th Birthday

Article directory

topic:

Input format:

Output format:

Input sample:

Sample output:

Problem solving code:

Problem-solving notes:


topic:

Gardon's 18th birthday is coming, of course he is very happy, but he suddenly thought of a question, does everyone have the same number of days from birth to 18th birthday? It doesn't seem to be the case at all, so he would like you to help me calculate the total number of days from birth to 18th birthday for him and a few of his friends, so that he can compare.

Input format:

An integer T, indicating the number of groups of test data, followed by T lines of dates, one for each line, the format is YYYY-MM-DD. For example, my birthday is 1988-03-07.

Output format:

Line T, one number per line, indicates the number of days that have elapsed from the person's birth to his 18th birthday. If the person does not have an 18th birthday, output -1.

Input sample:

1
1988-03-07

Sample output:

6574

Problem solving code:

#include<stdio.h>
int leap(int t)
{
    if((t%4==0&&t%100!=0)||t%400==0)
        return 1;
    return 0;
}
int main()
{
    int n,i,y,m,d,sum,x;
    scanf("%d",&n);
    while(n--)
    {
        sum=0;
        scanf("%d-%d-%d",&y,&m,&d);
        if(m==2&&d==29)
        { printf("-1\n");
        continue;
        }
        if(leap(y)&&m<3)
            sum++;
        x=y+18;
        if(leap(x)&&m>=3)
            sum++;
        for(i=y+1;i<y+18;i++)
            if(i%4==0&&i%100!=0||i%400==0)
                sum++;
        printf("%d\n",365*18+sum);
    }
    return 0;
}

Problem-solving notes:

1. Since the format given in the question is YYYY-MM-DD when inputting, add - between the numbers, otherwise the input data is garbled

scanf("%d-%d-%d",&y,&m,&d);

2. Pay attention to the number of days in February in the special month, and mark it with leap

int leap(int t)
{
    if((t%4==0&&t%100!=0)||t%400==0)
        return 1;
    return 0;
}

3. When a person is born, he is not 1 year old but 0 years old

Guess you like

Origin blog.csdn.net/weixin_63249578/article/details/128534256