Question
The Baidu server uses the Baidu time instead of Beijing time. The Baidu time is the same as the Beijing time, but the date is different from the Beijing time. A positive integer is used to indicate that the time has elapsed for several days since January 1, January 1, 2000.
Now, please design a program to convert Beijing time to Baidu time. In this question, the year of a leap year is a multiple of 400, or a multiple of 4, but not a multiple of 100. For example, 2000 and 8888 are both leap years, But 6100 is not.
Input Format
Each row of the input data is converted to Beijing time (excluding spaces and tabs). The correct format includes:
One is: YYYY-MM-DD, (yyyy indicates four-digit year, mm is two months, DD is two dates );
The other is mm/DD/YYYY (yyyy indicates the four-digit year, MM indicates the two-digit month, and DD indicates the two-digit date );
Output Format
Each data output line. If the conversion is successful, a positive integer is output; otherwise, an error is output.
Input example
2000-01-01
Astar2007
05/26/2007
Sample output
0
Error
2702
Scoring rules
The program will run on a Linux machine (the memory usage is not strictly limited). It cannot run more than 1 second in each test case. Otherwise, the test case will not be scored;
The program is required to read the standard input data according to the input sample format, and output the running results to the standard output according to the output sample format. If the data cannot be correctly read and output, the score is not calculated;
There are five test datasets in this question. All dates in data 1 and Data 2 can be converted successfully. Of all data, each row cannot exceed 20 characters, and each group of data can contain up to 100 rows;
This question has 20 points. My answer: # include <stdio. h>
# Include <string. h> int MD [] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31}; int baidudate (INT y, int M, int D)
{
Int bt = (Y-2000) * 365 + (Y-2000 + 1)/4;
For (INT I = 2000; I <Y; I = I + 100)
{
If (I % 4 = 0) & (I % 400! = 0 ))
{
Bt = bt-1;
}
}
For (Int J = 0; j <m-1; ++ J)
{
Bt + = md [J];
}
If (M> = 3)
{
If (Y % 400 = 0) | (Y % 100! = 0) & (Y % 4 = 0 )))
{
BT ++;
}
}
Bt + = D;
Return bt-1;
} Int main (void)
{
Int year, month, day;
Char s [81];
Int baidu_date; while (true)
{
Scanf ("% s", S );
Char * ptr1 = strchr (S ,'-');
If (! Ptr1) // not including '-'
{
Char * ptr2 = strchr (S ,'/');
If (! Ptr2) // not including '/'
{
Printf ("error/N ");
Continue;
}
Else
{
Sscanf (S, "% d/% d", & month, & day, & year );
}
}
Else
{
Sscanf (S, "% d-% d", & year, & month, & Day );
}
Baidu_date = baidudate (year, month, day );
Printf ("% d/N", year, month, day, baidu_date );
}
}