20th birthday
Time Limit: 1000 MS | memory limit: 65535 KB
Difficulty: 1
Description
The 20th birthday is coming soon. Of course, he is very happy, but he suddenly thought of a question, is it true that everyone starts from birth, what is the same number of days after the 20th birthday? This does not seem to be the case, so he would like to ask you to calculate the total number of days that he and several of his friends have passed since their birth to their 20th birthday, so that he can make a better comparison.
Input
A number T, followed by a date in each line of T, in the format of YYYY-MM-DD. For example, my birthday is 1988-03-07.
Output
T, each row has one number, indicating the number of days this person has passed from his birth to his/her 20th birthday. If this person does not have a 20th birthday, output-1.
Sample Input
1
Example 7305: first, calculate the total number of days from birth to birth, and then add the number of days from birth 2nd years to birth 19th years each year, in the end, how many days will it take to add his 20th birthday to his birthday. Note: (1) if the Year of birth is a leap year, but 20 years later is not a leap year, output-1; (2) the day of birth is the 0th day of birth.
Reference code:
[Cpp]
# Include <stdio. h>
Int m [13] = {, 31, 31 };
Int leapyear (int x) // determines the leap year.
{
If (x % 4 = 0 & x % 100! = 0) | x % 400 = 0)
Return 1;
Return 0;
}
Int count (int a, int B, int c) // judge the day of the year
{
Int s = 0, I;
If (a % 4 = 0 & a % 100! = 0 | a % 400 = 0)
{
For (I = 1; I <B; I ++)
S + = m [I];
If (B> 2)
Return s + c + 1; // note that the leap year is 29 days on January 1, February.
Else
Return s + c;
}
Else
{
For (I = 1; I <B; I ++)
S + = m [I];
Return s + c;
}
}
Int main ()
{
Int j, n, Y, M, D, sum, y, m, d;
Scanf ("% d", & n );
While (n --)
{
Scanf ("% d-% d", & Y, & M, & D );
If (M = 2 & D = 29 &&! Leapyear (Y + 20 ))
{
Printf ("-1 \ n ");
Continue;
}
Sum = 0;
If (leapyear (Y ))
Sum ++ = 366-count (Y, M, D );
Else
Sum ++ = 365-count (Y, M, D );
J = 1;
Y ++;
While (j <= 20)
{
If (leapyear (Y) & j! = 20)
Sum ++ = 366;
Else if (! Leapyear (Y) & j! = 20)
Sum ++ = 365;
Else if (j = 20)
Sum + = count (Y, M, D );
Y ++;
J ++;
}
Printf ("% d \ n", sum );
}
Return 0;
}
# Include <stdio. h>
Int m [13] = {, 31, 31 };
Int leapyear (int x) // determines the leap year.
{
If (x % 4 = 0 & x % 100! = 0) | x % 400 = 0)
Return 1;
Return 0;
}
Int count (int a, int B, int c) // judge the day of the year
{
Int s = 0, I;
If (a % 4 = 0 & a % 100! = 0 | a % 400 = 0)
{
For (I = 1; I <B; I ++)
S + = m [I];
If (B> 2)
Return s + c + 1; // note that the leap year is 29 days on January 1, February.
Else
Return s + c;
}
Else
{
For (I = 1; I <B; I ++)
S + = m [I];
Return s + c;
}
}
Int main ()
{
Int j, n, Y, M, D, sum, y, m, d;
Scanf ("% d", & n );
While (n --)
{
Scanf ("% d-% d", & Y, & M, & D );
If (M = 2 & D = 29 &&! Leapyear (Y + 20 ))
{
Printf ("-1 \ n ");
Continue;
}
Sum = 0;
If (leapyear (Y ))
Sum ++ = 366-count (Y, M, D );
Else
Sum ++ = 365-count (Y, M, D );
J = 1;
Y ++;
While (j <= 20)
{
If (leapyear (Y) & j! = 20)
Sum ++ = 366;
Else if (! Leapyear (Y) & j! = 20)
Sum ++ = 365;
Else if (j = 20)
Sum + = count (Y, M, D );
Y ++;
J ++;
}
Printf ("% d \ n", sum );
}
Return 0;
}