Confusing Date Format

来源:互联网 发布:幽浮2知乎 编辑:程序博客网 时间:2024/06/13 22:05
Reading date sometimes can be confusing, especially on “international” documents where we don’t
know the date format being used. For example, what does ‘04-10-11’ refer to? Is it 4 October 1911?
11 October 1904? 10 April 1911?
The basic components of a date are: D (day), M (month), and Y (year). Generally, there are three
date formats being used around the world:
• D-M-Y (little endian), e.g., 04-10-11 means 4 October 1911,
• Y-M-D (big endian), e.g., 04-10-11 means 11 October 1904,
• M-D-Y (middle endian), e.g., 04-10-11 means 10 April 1911.
For the purpose of this problem, we also consider these additional formats:
• D-Y-M, e.g., 04-10-11 means 4 November 1910,
• M-Y-D, e.g., 04-10-11 means 11 April 1910,
• Y-D-M, e.g., 04-10-11 means 10 November 1904.
Given a date “A-B-C” in unknown format (one of the above formats), determine how many different
dates are represented by it. Each of A, B, and C will be two digits integer. Only the last two digits of
the years will be given, and the year is between 1900 and 1999, inclusive.
Be careful, you also need to consider leap year in your solution. Definition of leap year is: “every
year that is exactly divisible by four is a leap year, except for years that are exactly divisible by 100,
but these centurial years are leap years if they are exactly divisible by 400” (taken from Wikipedia).
In case you don’t know how many days in a month, use the following table:
# Month Number of Days
01 January 31
02 February 28 (29 in leap year)
03 March 31
04 April 30
05 May 31
06 June 30
07 July 31
08 August 31
09 September 30
10 October 31
11 November 30
12 December 31
To punish teams who did not read this problem statement carefully, we’ll add one trick input: if the
input is “04-05-01”, the output should be 1 (not 6). Now, solve this problem as fast as possible! The
sooner you solve this problem, the lesser your time penalty for this problem will be, of course that is if
your solution is correct.
Input
The first line of input contains an integer T (T ≤ 100) denoting the number of cases. Each case will be
given in the following format: ‘A-B-C’ (00 ≤ A, B, C ≤ 99) representing a date in unknown format.
Each of A, B, and C will be two digits integer.
Output
For each case, output ‘Case #X: Y ’ (without quotes) in a line where X is the case number (starts
from 1), and Y is the answer for this particular case.
Explanation for the sample:
First case: This is the example given in the problem statement.
Second case: There is only one possible date: 15 February 1929 (Y-M-D). Note that 29 February
1915 is not a valid date as 1915 is a common year (not leap year), thus February 1915 has only 28 days.
Third case: There are no possible dates.
Fourth case: There are two possible dates: 2 January 1900 (D-Y-M) and 1 February 1900 (M-Y-D).
Sample Input
4
04-10-11
29-02-15
60-50-90
02-00-01
Sample Output
Case #1: 6
Case #2: 1
Case #3: 0

Case #4: 2

题意:给出3个数字,判断由这3个数字组成的日期是否存在

思路:照着题目模拟,只是需要注意三个数字中有2个或以上相同的情况,并且题目有给出的特殊的输入例子。

反思:1.在发现样例正确之后,提交了一次,WA,然后又考虑到一个月31日那里的判断写错了,还发现了判断闰年的时候并不是从1900开始的,之后又注意到题目给出的特殊的例子,WA了5次之后才正确,严谨的逻辑思维需要养成,尽量做到一交就正确,从出题人的角度看待问题

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;int num[10],re[10][5];int judgeyear(int n){    if(n%4==0&&n%100!=0)        return 1;    if(n%400==0)        return 1;    return 0;}int judgemonth(int n){    if(n==1||n==3||n==5||n==7||n==8||n==10||n==12)        return 1;    else if(n==2)        return 2;    return 3;}int main(){    int t,icase=0;    scanf("%d",&t);    while(t--)    {        int sum=0;        scanf("%d-%d-%d",&num[0],&num[1],&num[2]);        if(num[0]==4&&num[1]==5&&num[2]==1)               sum=1;        else{        for(int i=0;i<3;i++)//年        {            for(int j=0;j<3;j++) //月            {                if(i!=j)                {                    for(int k=0;k<3;k++) //日                    {                        if(k!=i&&k!=j)                        {                          if(0<=num[i]&&num[i]<=99){                           int  flag=judgeyear(1900+num[i]);//判断是否是闰年 1 是 0 否                            if(1<=num[j]&&num[j]<=12) //月份                            {                                int month=judgemonth(num[j]),day;                                if(month==1)                                    day=31;                                else if(month==2)                                {                                    if(flag==1)   day=29;                                    else          day=28;                                }                                else if(month==3)                                    day=30;                                //分类月份                                if(1<=num[k]&&num[k]<=day)                                {                                    int judge=1;                                    sum++;                                    for(int m=1;m<sum;m++)                                    {                                        if(num[i]==re[m][0]&&num[j]==re[m][1]&&num[k]==re[m][2])                                            {                                              sum--;                                              judge=0;                                               break;                                            }                                    }                                    if(judge)                                    {                                        re[sum][0]=num[i],re[sum][1]=num[j],re[sum][2]=num[k];                                        //printf("%d %d %d\n",num[i],num[j],num[k]);                                    }                                }                                else                                    break;                            }                            else   break;                          }                        }                    }                }              }        }        }        printf("Case #%d: %d\n",++icase,sum);    }    return 0;}


原创粉丝点击