电梯调度算法(C#实现)

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