C#逻辑面试题汇总【不断更新中】


(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);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值