(1)产生本月的月历,参考样式:
1
2
3
4
5
6
|
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
|
将结果存入一个字符串里面
框架程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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);
}
}
}
|
时间限制 1 小时。
===============================================================
答案区:
(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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);
|