日期类问题 3-28

题目描述

有两个日期,求两个日期之间的天数,如果两个日期是连续的我们规定他们之间的天数为两天。

 

 

输入

有多组数据,每组数据有两行,分别表示两个日期,形式为YYYYMMDD

 

 

输出

每组数据输出一行,即日期差值

 

 

样例输入
20130101

20130105




思路如下:
输入两个日期,用日期小的逐日增加,直到增加到日期大的位置,求出所得天数。
 1 #include<stdio.h>
 2 #define ISYEAR(x) (x%4==0 && x%100 != 0) || x%400 == 0 ? 1 : 0
 3 int dayOfmonth[13][2] = 
 4 {
 5     0,0,
 6     31,31,
 7     28,29,
 8     31,31,
 9     30,30,
10     31,31,
11     30,30,
12     31,31,
13     31,31,
14     30,30,
15     31,31,
16     30,30,
17     31,31
18 };
19 struct date{
20     int year;
21     int month;
22     int day;
23 };
24 int nextday(date a, date b)
25 {
26     int count = 1;
27     while(a.day != b.day || a.year != b.year || a.month != b.month)
28     {
29         a.day++;
30         if(a.day > dayOfmonth[a.month][ISYEAR(a.year)])
31         {
32             a.day = 1;
33             a.month ++;
34             if(a.month > 12)
35             {    
36                 a.year ++;
37                 a.month = 1;
38             }
39         }
40         count ++;
41     }
42     return count;
43 }
44 int main()
45 {
46     date x,y;
47     while(scanf("%4d%2d%2d",&x.year,&x.month,&x.day) != EOF)
48     {
49         scanf("%4d%2d%2d",&y.year,&y.month,&y.day);
50         date tmp;
51         if(x.year > y.year)
52         {
53             tmp = x;
54             x = y;
55             y = tmp;
56         }
57         if(x.year == y.year)
58         {
59             if(x.month > y.month)
60             {
61                 tmp = x;
62                 x = y;
63                 y = tmp;
64             }
65         }
66         if(x.year == y.year && x.month == y.month)
67         {
68             if(x.day > y.day)
69             {
70                 tmp = x;
71                 x = y;
72                 y = tmp;
73             }
74         }
75         int dis = nextday(x,y);
76         printf("%d\n",dis);
77     }
78     return 0;
79 }

 


扩展

2008年上海交通大学计算机研究生机试真题

题目描述

We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.

 

 


输入

There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.

 

 


输出

Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.

 

 


样例输入
21 December 2012
5 January 2013
 
 
样例输出
Friday
Saturday
 
思路,利用上一题的结果算出,要求日期距离今日的总日期,然后对7取模运算,然后可得所给日期是星期几。
 
  1 #include<stdio.h>
  2 #include<cstring>
  3 #define ISYEAR(x) (x%4==0 && x%100 != 0) || x%400 == 0 ? 1 : 0
  4 int dayOfmonth[13][2] = 
  5 {
  6     0,0,
  7     31,31,
  8     28,29,
  9     31,31,
 10     30,30,
 11     31,31,
 12     30,30,
 13     31,31,
 14     31,31,
 15     30,30,
 16     31,31,
 17     30,30,
 18     31,31
 19 };
 20 struct date{
 21     int year;
 22     int month;
 23     int day;
 24 };
 25 int nextday(date a, date b)
 26 {
 27     int count = 0;
 28     while(a.day != b.day || a.year != b.year || a.month != b.month)
 29     {
 30         a.day++;
 31         if(a.day > dayOfmonth[a.month][ISYEAR(a.year)])
 32         {
 33             a.day = 1;
 34             a.month ++;
 35             if(a.month > 12)
 36             {    
 37                 a.year ++;
 38                 a.month = 1;
 39             }
 40         }
 41         count ++;
 42     }
 43     return count;
 44 }
 45 char in_week[7][10] = {
 46     "Monday",
 47     "Tuesday",
 48     "Wednesday",
 49     "Thursday",
 50     "Friday",
 51     "Saturday",
 52     "Sunday"
 53     };
 54 int main()
 55 {
 56     date x,y;
 57     char s[10];
 58     while(scanf("%d%s%d",&x.day,s,&x.year) != EOF)
 59     {
 60         if(strcmp(s,"January") == 0)
 61             x.month = 1;
 62         if(strcmp(s,"February") == 0)
 63             x.month = 2;
 64         if(strcmp(s,"March") == 0)
 65             x.month = 3;
 66         if(strcmp(s,"April") == 0)
 67             x.month = 4;
 68         if(strcmp(s,"May") == 0)
 69             x.month = 5;
 70         if(strcmp(s,"June") == 0)
 71             x.month = 6;
 72         if(strcmp(s,"July") == 0)
 73             x.month = 7;
 74         if(strcmp(s,"August") == 0)
 75             x.month = 8;
 76         if(strcmp(s,"September") == 0)
 77             x.month = 9;
 78         if(strcmp(s,"October") == 0)
 79             x.month = 10;
 80         if(strcmp(s,"November") == 0)
 81             x.month = 11;
 82         if(strcmp(s,"December") == 0)
 83             x.month = 12;
 84         y.day = 28;
 85         y.year = 2013;
 86         y.month = 3;
 87         date tmp;
 88         bool flag = true;
 89         if(x.year > y.year)
 90         {
 91             tmp = x;
 92             x = y;
 93             y = tmp;
 94             flag = false;
 95         }
 96         if(x.year == y.year)
 97         {
 98             if(x.month > y.month)
 99             {
100                 tmp = x;
101                 x = y;
102                 y = tmp;
103                 flag = false;
104             }
105         }
106         if(x.year == y.year && x.month == y.month)
107         {
108             if(x.day > y.day)
109             {
110                 tmp = x;
111                 x = y;
112                 y = tmp;
113                 flag = false;
114             }
115         }
116         int dis = nextday(x,y);
117         if(flag == true)
118         {
119             printf("%s",in_week[6 - (dis + 3)%7]);
120         }
121         else
122             printf("%s",in_week[(dis + 3)%7]);
123         printf("\n");
124     }
125     return 0;
126 }

 

 
 
 

posted on 2013-03-29 10:37  lgy111  阅读(175)  评论(0)    收藏  举报

导航