Title Description:
Gardon's 18 birthday is coming, he is certainly very happy, but he suddenly thought of a problem, is not everyone from birth, to reach the 18-year-old birthday when the number of days are the same. It doesn't seem to be all that, so he wants to ask you to calculate the total number of days he and his friends have passed since they were born to their 18 birthday.
Input:
A number T, followed by the T line, has a date on each line, and the format is YYYY-MM-DD. As my birthday is in 1988-03-07.
Output:
T line, one number per line, indicating the number of days that the person has passed from birth to the age of 18. If this person has no 18 birthday, then output-1
Sample input:
1
1988-03-07
Sample output:
6574
Problem Solving Ideas:
This problem is not very difficult, first of all, consider this person has no 18-year-old birthday situation. It is clear that only February 29 births may be born, and the 18th year after birth must not be leap years (18 is not a multiple of 4). Therefore, the person born on February 29 did not have a 18 birthday, output-1.
Consider not the February 29 birth situation, the birthday of the year to the second birthday of the number of days passed, not 365 days, or 366 days. The reason that sometimes passes 366 days is because the leap year in February is more than a day.
If the person's birthday is in March and after, then if the second year is a leap years, he will go through 366 days to the second birthday (because after the February 29, 2), otherwise it is after 365 days; If the person's birthday is before March, then if the current year is a leap year, to the second birthday, Will pass through 366 days (because of the current year's February 29), otherwise after 365 days.
So, this person's birthday is not the premise of February 29, first determine the person's birthday is before March, or after March, the birthday month before March, the current year is the number of days after the birthday of the second year. Birthday month after March, the number of days after the next year is the number of days after the birthday of the second year.
#include <stdio.h>
int leapyear (int year)
{
if (year%4==0 && year%100!=0 | | year%400==0) return 1;
else return 0;
}
A custom function that determines whether the year is a leap or common year
int main ()
{
int t,y,year,month,day;
int sum;
Variable sum counts the number of days
scanf ("%d", &t) passed from birth to age 18
; while (t--)
{
scanf ("%d-%d-%d", &year,&month,&day);
Enter the birthday of the year, month, day
if (month==2 && day==29) printf (" -1\n"); Born February 29, then output-1
else
{
sum=0;
if (month>=3) //month is greater than 2, the year after which the flat or run determines the number of days spent on the first year of life
{for
(y=year+1;y<=year+18;y++) // Note that the range in the for loop is shifted backwards by one year
{
if (leapyear (y)) sum+=366;
else sum+=365;
}
}
else if (month<=2) //month is less than or equal to 2, the year of the flat or run determines the number of days spent on the first year
{for
(y=year;y<=year+17;y++) // Note the range in the For loop, the current year
{
if (leapyear (y)) sum+=366;
else sum+=365;
}
}
printf ("%d\n", sum);
}
}
return 0;
}