elevator design example
来源:互联网 发布:linux给用户赋root权限 编辑:程序博客网 时间:2024/06/03 12:33
转自:http://careercup.com/question?id=5698327039442944
repose with format corrected*public class Elevator {public static final int MAX_FLOORS = 50;// upward floor queueprivate PriorityQueue<Integer> floors_up = new PriorityQueue<Integer>(MAX_FLOORS);// downward floor queueprivate PriorityQueue<Integer> floors_down = new PriorityQueue<Integer>(MAX_FLOORS, new Comparator<Integer>(){public int compare(Integer arg0, Integer arg1) {return arg1.compareTo(arg0);}});// effective floor queueprivate PriorityQueue<Integer> floors = floors_up;// current floorprivate int current = 0;public void userCallAt(int where) {setTarget(where);}public void setTarget(int target) {// ignore if target is currentif (target == current) {return;}// add target to right queueif (floors == floors_up) {if (target < current) {floors_down.offer(target);} else {floors_up.offer(target);}} else {if (target > current) {floors_up.offer(target);} else {floors_down.offer(target);}}// swap queue to turn around the elevatorif (floors.isEmpty()) {floors = (floors == floors_up ? floors_down : floors_up);}}public int getTarget() {if (!floors.isEmpty()) {return floors.peek();}if (floors_up.isEmpty() && floors_down.isEmpty()) {return current;}floors = (floors == floors_up ? floors_down : floors_up);return floors.peek();}public int getCurrent() {return current;}public void step(int target) {if (target > current) {current++;} else {current--;}}public void stop() {floors.poll();}public void move() {while (getCurrent() != getTarget()) {do {step(getTarget());} while (getCurrent() != getTarget());stop();}}}
0 0
- elevator design example
- 使用 IP example design
- Design elevator(include class, logic, etc)
- Design Patterns - Command - Example 1
- Design Patterns - Decorator - Example 1
- Design Patterns - Decorator - Example 2
- Design Patterns - Decorator - Example 3
- Android Material Design Snackbar Example
- Design Patterns used in actual Example
- Design Patterns - Abstract Factory - Example 1
- Design Patterns - Factory Method - Example 1
- Java Design Patterns – Example Tutorial
- Elevator
- Elevator
- Elevator
- elevator
- Elevator
- Elevator
- VB 当控件被数据绑定时,无法以编程方式向 DataGridView 的行集合中添加行
- bzoj 1449 && bzoj 2597 - 一类凸费用网络流
- Swift_DayOne
- js实现瀑布流效果(使用绝对定位)
- 优先队列的实现及其在哈夫曼编码中的应用
- elevator design example
- 指针相关的预定义类型
- 获取访问者真实的IP地址(避免反向代理的影响)
- 89. Gray Code LeetCode
- Python chr ord
- Android 第十天
- EventBus源码研读(上)
- DP算法之整数划分
- EventBus源码研读(中)