日期:2014-05-20 浏览次数:20933 次
SU MO TU WE TH FR SA 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string calendar = ""; // 在这里编写代码 Console.WriteLine(calendar); } } }
string calendar = ""; // 在这里编写代码 DateTime now = DateTime.Today; DateTime dt1 = new DateTime(now.Year, now.Month, 1); DateTime dt2 = dt1.AddMonths(1).AddDays(-1); StringBuilder sb = new StringBuilder(); for (int i = (int)DayOfWeek.Sunday; i <= (int)DayOfWeek.Saturday; i++) { sb.Append(((DayOfWeek)i).ToString().Substring(0, 2).ToUpper() + " "); } for (DateTime dt = dt1.AddDays(-(int)now.DayOfWeek); dt <= dt2; dt = dt.AddDays(1)) { if (dt.Month < now.Month) sb.Append(" "); else sb.Append(dt.Day.ToString().PadLeft(2, '0') + " "); if (dt.DayOfWeek == DayOfWeek.Saturday) sb.AppendLine(); } calendar = sb.ToString(); Console.WriteLine(calendar);
------解决方案--------------------
方法笨点。。
string calendar = ""; // 在这里编写代码 DateTime date = DateTime.Now; DateTime firstDay = new DateTime(date.Year, date.Month, 1); int daysCount = DateTime.DaysInMonth(date.Year, date.Month); int weekOfFirstDay = Convert.ToInt32(firstDay.DayOfWeek); calendar = "\tSU\tMO\tTU\tWE\tTH\tFR\tSA\n"; for (int i = 1; i < daysCount + 1; i++) { if (i == 1) { for (int j = 0; j < weekOfFirstDay; j++) { calendar += "\t"; } } string str = i < 10 ? i.ToString().PadLeft(2, '0') : i.ToString(); calendar += "\t" + str; if ((i + weekOfFirstDay) % 7 == 0) { calendar += "\n"; } } Console.WriteLine(calendar); Console.ReadLine();
------解决方案--------------------
拿出手机,调出日历,照着画
Console.WriteLine("SU MO TU WE TH FR SA");
Console.WriteLine(" 01 02 03 04");
Console.WriteLine("05 06 07 08 09 10 11");
Console.WriteLine("12 13 14 15 16 17 18");
Console.WriteLine("19 20 21 22 23 24 25");
Console.WriteLine("26 27 28 29 30 ");
临走别忘记淡定的说句,"这公司太弱了,出这么简单的题目,老子来了一次不想来第二次了"
------解决方案--------------------
动动手,写个:
static void GenerateCalendar(int year, int month) { var num = 7; var cols = Enumerable.Range(0, num).ToArray(); var row = Enumerable.Range(0, num).Select(x => "__").ToArray(); var begin = new DateTime(year, month, 1); var end = begin.AddDays(DateTime.DaysInMonth(year, month) - 1); while (begin.Month == end.Month && begin.Day <= end.Day) { row[(int)begin.DayOfWeek] = begin.Day.ToString("00"); if ((int)begin.DayOfWeek == cols[cols.Length-1]) { Console.WriteLine(string.Join(" ", row)); row = Enumerable.Range(0, num).Select(x => "__").ToArray(); } begin = begin.AddDays(1); } Console.WriteLine(string.Join(" ", row)); }