银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::
Timus 1201. Which day is it? 要求输出指定日期的月历。

1201. Which day is it?

Time Limit: 1.0 second
Memory Limit: 16 MB
Sometimes it is of great importance to know which day of the week a given date will be. And we start searching for the nearest calendar. Being lucky we may find one. And find out that this one does not contain the date you need. What a pity!
Thus you are asked to create a calendar that will be able to process any given date in the years range from 1600 till 2400. Given a date, your program should print (see the examples below) a correct calendar for the month containing the date. Do not forget about the leap years. A year is considered to be leap if it is multiple of 4 except it is multiple of 100 except it is multiple of 400. For example 1996 is a leap year, 1900 is not a leap year (it is a multiple of 4 and multiple of 100) and 2000 is a leap year (it is a multiple of 4, multiple of 100 and multiple of 400 as well).

Input

The first line of input contains a date, i.e. three integer numbers: day (1–31), month (1–12) and year (1600–2400) separated by spaces.

Output

The output should contain exactly 7 lines with the correct calendar for the month containing the given date. Format of a calendar is given by the examples below (for a reading convenience spaces in output example are replaced with dots, real output should contain spaces instead). And do not forget to highlight the given date by square brackets.

Samples

inputoutput
16 3 2002
mon........4...11...18...25
tue........5...12...19...26
wed........6...13...20...27
thu........7...14...21...28
fri...1....8...15...22...29
sat...2....9..[16]..23...30
sun...3...10...17...24...31
         
1 3 2002
mon........4...11...18...25
tue........5...12...19...26
wed........6...13...20...27
thu........7...14...21...28
fri.[.1]...8...15...22...29
sat...2....9...16...23...30
sun...3...10...17...24...31
         
Problem Author: Alexander Klepinin
Problem Source: USU Internal Contest, March 2002

解答如下:

 1 using System;
 2 
 3 namespace Skyiv.Ben.Timus
 4 {
 5   // http://acm.timus.ru/problem.aspx?space=1&num=1201
 6   sealed class T1201
 7   {
 8     static void Main()
 9     {
10       var ss = Console.ReadLine().Split();
11       var dt = new DateTime(int.Parse(ss[2]), int.Parse(ss[1]), int.Parse(ss[0]));
12       var d0 = dt.AddDays(1 - dt.Day);
13       var k = -(((int)d0.DayOfWeek + 6% 7);
14       string[] weeks = {"mon""tue""wed""thu""fri""sat""sun"};
15       for (var i = 0; i < weeks.Length; i++)
16       {
17         Console.Write(weeks[i]);
18         for (var d = d0.AddDays(k + i); d < d0.AddMonths(1); d = d.AddDays(7))
19           Console.Write((d < d0) ? "    " : ((d != dt) ?
20             (((d >= d0.AddDays(k + 7&& d != dt.AddDays(7)) ? " " : ""+ "{0,4}") :
21             (((d >= d0.AddDays(k + 7))? " " : ""+ " [{0,2}]")), d.Day);
22         Console.WriteLine();
23       }
24     }
25   }
26 }

这道题目相当的简单,就是要注意一下输出的格式,特别是输入的日期那天的格式。

这道题目输入日期的范围是从1600年到2400年,我想是为了避开以下问题:1582年10月4日的下一天是1582年10月15日

这个程序来源于 CSDN 论坛上 止戈(min_jie) 朋友的一个帖子:“竟然会超时,WHY?”,是对该帖子中23楼的程序进行修改和重构而得到的。

这个程序可以进一步简化,第 19 到 21 行的语句可以用以下一行替换:

Console.Write((d < d0) ? "     " : ((d != dt) ? "{0,4} " : " [{0,2}]"), d.Day);

注意,这已经不是严格意义上的重构了,实际上程序的输出改变了,在每行的最后一列后面加上了一个空格(除了该行的最后一个字符是右方括号“]”的情况以外)。虽然这样一来,这个程序的输出和题目的例子中的输出不一样,但这样更简单,更符合逻辑,因为这样每行的字符数就都一样了,并且每一列的字符数也都一样了(都是五个字符)。如果按照题目的例子,如果右方括号“]”是某一行的最后一个字符的话,该行的字符数就会比其他行多一个。最重要的,修改后的程序能够通过 Timus 系统的检验,结果是 Accept。


返回目录
posted on 2009-03-12 23:06  银河  阅读(1734)  评论(3编辑  收藏  举报