Timus 1201. Which day is it? 要求输出指定日期的月历。
Problem Author: Alexander Klepinin
Problem Source: USU Internal Contest, March 2002
返回目录
1201. Which day is it?
Time Limit: 1.0 second
Memory Limit: 16 MB
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
input | output |
---|---|
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 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 }
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 行的语句可以用以下一行替换:
注意,这已经不是严格意义上的重构了,实际上程序的输出改变了,在每行的最后一列后面加上了一个空格(除了该行的最后一个字符是右方括号“]”的情况以外)。虽然这样一来,这个程序的输出和题目的例子中的输出不一样,但这样更简单,更符合逻辑,因为这样每行的字符数就都一样了,并且每一列的字符数也都一样了(都是五个字符)。如果按照题目的例子,如果右方括号“]”是某一行的最后一个字符的话,该行的字符数就会比其他行多一个。最重要的,修改后的程序能够通过 Timus 系统的检验,结果是 Accept。
返回目录
分类:
算法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述