Beauty of the process-elevator degree algorithm and elevator Scheduling Algorithm
Recently, I want to study the elevator Scheduling Algorithm in detail, but it involves too much content and there is no time. Types of elevator algorithms:
Http://www.cnblogs.com/jianyungsun/archive/2011/03/16/1986439.html
Then I can only study the simplicity.
This is a question in the beauty of programming:
There are 6 elevators in a building. During peak hours, there are people on and off each layer, and the elevator stops on each layer.
Intern Xiaofei is often impatient with the elevator on every floor, so he proposed the following method:
Because the floor is not too high, so in the busy working hours, every time the elevator goes up from the first floor, we only allow the elevator to stop on the first floor. All passengers are taken from the elevator upstairs. After arriving at a certain floor, the elevator stops and all passengers climb from here to their destination layer. When on the first floor of the elevator, each passenger chooses his or her target layer, and the elevator automatically calculates the floor to be stopped.
Q: Which floor does the elevator stop on? The minimum number of floors of all passengers on the elevator climb the stairs is guaranteed.
Paste the code I implemented in java:
Public static void main (String [] args) {for (int n = 0; n <10; n ++) {int nums [] = getInfoInOneElevator (); System. out. println ("Total:" + nums. length + "number of people going upstairs, respectively:"); for (int I = 0; I <nums. length; I ++) {System. out. print (nums [I] + "");} System. out. println (""); int [] info = getMinTotalSteps (nums); System. out. println ("minimum steps:" + info [0]); System. out. println ("stop at floor:" + info [1]); System. out. println ("---------------------------") ;}/ *** calculate the minimum number of steps required to climb according to the information of the person in the elevator, and the floor where the elevator should stop */public static int [] getMinTotalSteps (int nums []) {int minStep = level * nums. length; // int nFloor =-1; for (int I = 0; I <level; I ++) {int tempStep = 0; for (int j = 0; j <nums. length; j ++) {tempStep + = Math. abs (nums [j]-I-1);} if (minStep> tempStep) {minStep = tempStep; nFloor = I + 1 ;}} return new int [] {minStep, nFloor};}/*** random number of people on the elevator, limited to manned 13 */public static int [] getInfoInOneElevator () {// number of random numbers int [] nums = new int [(int) (Math. random () * 13)]; for (int I = 0; I <nums. length; I ++) {nums [I] = (int) (Math. random () * level) + 1;} return nums ;}
After 10 random simulations:
This problem is relatively simple. In the future, we will have time to simulate various elevator Scheduling Algorithms in detail.