电梯调度算法的实现

View Code
  1 class SCAN
  2     {
  3 
  4         static void Main(string[] args)
  5         {
  6             SCAN s = new SCAN();
  7 
  8             int[] a = { 55, 58, 39, 18, 90, 160, 150, 38, 184 };
  9             int nowway = 100;
 10 
 11 
 12             int[] high = s.high(nowway, a);//高磁道
 13             int hl = high.Length;
 14 
 15 
 16 
 17             int[] low = s.low(nowway, a);//低磁道
 18             int ll = low.Length;
 19 
 20 
 21             int[] all = new int[hl + ll];
 22             for (int i = 0; i < hl; i++)
 23             {
 24                 all[i] = high[i];
 25 
 26             }
 27             for (int i = 0, j = 0; i < ll; i++, j++)
 28             {
 29                 all[hl + i] = low[j];
 30             }
 31 
 32 
 33             int[] movelength = new int[all.Length];
 34             for (int i = 0; i < all.Length; i++)
 35             {
 36                 Console.WriteLine(all[i] + "      ");
 37                 int move = 0;
 38                 if (i == 0)
 39                 {
 40                     move = all[0] - 100;
 41                     movelength[i] = move;
 42                     Console.WriteLine("移动磁道数 " + move);
 43                     continue;
 44 
 45                 }
 46                 if (all[i] > all[i - 1])
 47                 {
 48                     move = all[i] - all[i - 1];
 49                     movelength[i] = move;
 50 
 51                 }
 52                 else
 53                 {
 54                     move = all[i - 1] - all[i];
 55                     movelength[i] = move;
 56                 }
 57 
 58                 Console.WriteLine("移动磁道数 " + move);
 59 
 60 
 61             }
 62 
 63             int summovelentgth = 0;
 64             for (int i = 0; i < movelength.Length; i++)
 65             {
 66                 summovelentgth += movelength[i];
 67 
 68 
 69             }
 70             Console.WriteLine("平均寻道长度:" + (double)summovelentgth / movelength.Length);
 71 
 72 
 73 
 74 
 75         }
 76 
 77         
 78         public int[] low(int nowway, int[] a)
 79         {
 80             int mincount = 0;
 81             for (int i = 0; i < a.Length; i++)
 82             {
 83                 if (a[i] < nowway)
 84                 {
 85                     mincount++; //小于磁道数的
 86                 }
 87             }
 88 
 89             int[] a1 = new int[mincount];
 90             int j = 0;
 91 
 92             for (int i = 0; i < a.Length; i++)
 93             {
 94                 if (a[i] < nowway)
 95                 {
 96 
 97                     a1[j] = a[i]; //小于磁道数的
 98                     j++;
 99                 }
100             }
101 
102             Array.Sort(a1);//对小磁道升序排列
103             int k = a1.Length;
104             int[] a3 = new int[k];
105             k--;
106             for (int i = 0; i < a1.Length; i++, k--)
107             {   //降序排列
108                 a3[i] = a1[k];
109 
110             }
111 
112 
113             return a3;
114         }
115         public int[] high(int nowway, int[] a)
116         {
117 
118             int maxcount = 0;
119 
120             for (int i = 0; i < a.Length; i++)
121             {
122                 if (a[i] > nowway)
123                 {
124 
125                     maxcount++; //大于磁道数的
126                 }
127             }
128 
129             int[] a2 = new int[maxcount];
130             int j = 0;
131             int m = 0;
132             for (int i = 0; i < a.Length; i++)
133             {
134                 if (a[i] > nowway)
135                 {
136 
137                     a2[m] = a[i]; //大于磁道数的
138                     m++;
139                 }
140             }
141 
142             Array.Sort(a2);//对大磁道升序排列
143             return a2;
144         }
145 
146 
147 
148 
149 
150     }

最近加班真是苦啊!!还有一个月回北京

posted @ 2012-07-26 20:46  有态度的80后  阅读(460)  评论(0编辑  收藏  举报