电梯调度算法(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 }

转载于:https://www.cnblogs.com/xinshijie/archive/2012/07/26/2610686.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的电梯调度算法的C语言实现,仅供参考: ```c #include <stdio.h> #define MAX 10 // 电梯最大承载量 #define FLOORS 20 // 楼层数 int floors[FLOORS] = {0}; // 记录每层楼的乘客数 int direction = 1; // 记录电梯的运行方向,1表示上行,-1表示下行 int current_floor = 0; // 记录电梯当前所在楼层 int passengers = 0; // 记录电梯内的乘客数 // 电梯上行 void go_up() { // 如果电梯到达顶层,改变运行方向 if (current_floor == FLOORS - 1) { direction = -1; return; } // 否则到达下一层楼 current_floor++; // 如果有乘客需要在该楼层上行,让他们上电梯 while (floors[current_floor] > 0 && passengers < MAX) { floors[current_floor]--; passengers++; } } // 电梯下行 void go_down() { // 如果电梯到达底层,改变运行方向 if (current_floor == 0) { direction = 1; return; } // 否则到达下一层楼 current_floor--; // 如果有乘客需要在该楼层下行,让他们上电梯 while (floors[current_floor] < 0 && passengers < MAX) { floors[current_floor]++; passengers++; } } int main() { int target_floor; while (1) { // 模拟电梯的运行 if (direction == 1) { go_up(); } else { go_down(); } // 打印电梯信息 printf("Current floor: %d\n", current_floor); printf("Passengers: %d\n", passengers); printf("Direction: %s\n", direction == 1 ? "up" : "down"); // 让乘客输入目标楼层 printf("Please enter the target floor (or -1 to exit): "); scanf("%d", &target_floor); // 如果输入-1,退出程序 if (target_floor == -1) { break; } // 如果电梯已满,拒绝新的乘客 if (passengers == MAX) { printf("The elevator is full. Please wait for the next one.\n"); continue; } // 记录乘客的目标楼层 floors[target_floor]++; } return 0; } ``` 这个算法模拟了一个简单的电梯调度过程,根据每个乘客的目标楼层,让电梯前往相应的楼层。在达到每个楼层时,如果有需要上行或下行的乘客,就让他们上电梯,直到电梯满员或没有更多的乘客需要上行或下行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值