一个打印螺旋数的程序

看到博客园上有人发这个程序的练习,便自己也写了一个。

ExpandedBlockStart.gif 代码
 1  class  ScremNumbers
 2  {
 3       enum  Direction { Right, Down, Left, Up }
 4 
 5      [System.Diagnostics.DebuggerDisplay( " ({X},{Y}) " )]
 6       struct  Point
 7      {
 8           public   int  X;
 9           public   int  Y;
10      }
11 
12       public   int  Count {  get private   set ; }
13 
14       int [,] numbers;
15       public  ScremNumbers( int  count)
16      {
17           this .Count  =  count;
18          numbers  =   new   int [count, count];
19 
20          Process();
21      }
22 
23       void  Process()
24      {
25          var point  =   new  Point();
26          var direction  =  Direction.Right;
27 
28           for  ( int  i  =   0 ; i  <  Count  *  Count; i ++ )
29          {
30              var nextPoint  =  NextPoint(point, direction);
31               if  ( this [nextPoint]  !=   0 )
32              {
33                  direction  =  NextDirection(direction);
34                  nextPoint  =  NextPoint(point, direction);
35              }
36 
37               this [point]  =  i  +   1 ;
38              point  =  nextPoint;
39          }
40      }
41 
42       public   void  Print()
43      {
44          var maxLength  =  (Count  *  Count).ToString().Length  +   1 ;
45 
46           for  ( int  i  =   0 ; i  <  Count; i ++ )
47          {
48               for  ( int  j  =   0 ; j  <  Count; j ++ )
49              {
50                  Console.Write(numbers[j, i].ToString().PadRight( 4 ));
51              }
52              Console.WriteLine();
53          }
54      }
55 
56      Point NextPoint(Point point, Direction direction)
57      {
58          var nextPoint  =  point;
59           switch  (direction)
60          {
61               case  Direction.Right:
62                  nextPoint.X ++ ;
63                   break ;
64               case  Direction.Down:
65                  nextPoint.Y ++ ;
66                   break ;
67               case  Direction.Left:
68                  nextPoint.X -- ;
69                   break ;
70               case  Direction.Up:
71                  nextPoint.Y -- ;
72                   break ;
73               default :
74                   throw   new  InvalidOperationException();
75          }
76 
77           return  nextPoint;
78      }
79 
80      Direction NextDirection(Direction direction)
81      {
82           return  (Direction)((( int )direction  +   1 %   4 );
83      }
84 
85       private   int   this [Point index]
86      {
87           get
88          {
89               if  (index.X  <   0   ||  index.X  >=  Count  ||  index.Y  <   0   ||  index.Y  >=  Count)
90                   return   - 1 ;
91 
92               return  numbers[index.X, index.Y];
93          }
94 
95           set  { numbers[index.X, index.Y]  =  value; }
96      }
97  }

 

程序用的算法比较简单,是一种最直接的算法:不停的往前走,碰壁便转弯。总体感觉整体思路还比较清晰,这里记录一下。

 

转载于:https://www.cnblogs.com/TianFang/archive/2010/12/27/1918330.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值