The beauty of the ﹣ electric ladder adjustment algorithm

Source: Internet
Author: User

Recently want to study in detail the elevator scheduling algorithm, but the content involved too much, there is no time. About the types of elevator algorithms:

Http://www.cnblogs.com/jianyungsun/archive/2011/03/16/1986439.html

Then I can only look at the simple.

This is a topic in the beauty of programming:

A total of 6 elevators are in a building. At rush hour, everyone on each floor is up and down, and the elevator stops on each floor.

Interns are often very impatient with the elevators that stop at each floor, so he puts forward such a way:

Since the floors are not too high, we only allow the elevators to stop at one of the floors when the elevator goes up and down from the first floor during rush hours. All the passengers from the first floor elevator, arrived at a floor, the elevator stopped, all the passengers from here to climb to their own destination layer. On the first floor of the elevator, each passenger chooses his own destination, and the elevator automatically calculates the floor to be stopped.

Q: In which floor the elevator is parked, it ensures that all passengers on this ride have the least number of floors to climb the stairs.

Just paste out the code I implemented in Java:

public static void Main (string[] args) {for (int n = 0; n <; n++) {int nums[] = Getinfoinoneelevator (); System.out.println ("Altogether have:" + nums.length + "people upstairs, respectively on the floor number:"); for (int i = 0; i < nums.length; i++) {System.out.print (nums[ I] + "");} System.out.println (""); int[] info = getmintotalsteps (nums); SYSTEM.OUT.PRINTLN ("Minimum number of steps:" + info[0]); System.out.println ("Stop at the floor:" + info[1]); System.out.println ("-----------------------------");}} /** * Based on the information of an elevator, calculate the minimum number of steps to climb, and elevator should stop the floor */public static int[] getmintotalsteps (int nums[]) {int minstep = level * Nums.lengt H The default is to go to the highest layer int nfloor = -1;for (int i = 0; I < level; i++) {int tempstep = 0;for (int j = 0; J < Nums.length; J + +) {tem Pstep + = Math.Abs (Nums[j]-i-1);} if (Minstep > Tempstep) {minstep = Tempstep;nfloor = i + 1;}} return new int[] {minstep, nfloor};} /** * Randomly get a group of people on the elevator, limited to manned */public static int[] Getinfoinoneelevator () {//random number int[] Nums = new int[(int) (Math.random () *];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, later time to detailed simulation of the various elevator scheduling algorithm.

The beauty of the ﹣ electric ladder adjustment algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.