Beauty of programming _ 006 Xiaofei's elevator Scheduling Algorithm

Source: Internet
Author: User
// Which floor does the elevator stop on? Can the minimum number of floors of all passengers who climb the stairs of the elevator be reached? Public class test_005 {// person [I] indicates the number of people to reach the I-level. person [0], person [1] is blank by default, that is, no one goes to layer 1 static int [] person = {0, 0, 5, 7, 8, 9, 6, 6, 1, 4, 4, 8, 5, 2, 4, 5, 8, 6, 3, 3, 5, 9, 9, 6, 6, 9, 8, 5, 5, 9, 6, 6, 3}; static int stopfloor = 0; // number of floors stopped by the elevator static int sumfloor = 0; // total number of floors crawled by a passenger static int n = person. length-1; // total number of floors static int n1 = 0; // number of people leaving the elevator to stay under the floor static int n2 = 0; // The number of people who leave the elevator to stay on the floor. Static int N3 = 0; // Number of people who leave the elevator on the floor/*** calculate the target floor * @ Param person // record the number of people to reach each floor */public static void targetfloor (INT [] person) {// if there is no more than 1 floor, you do not need to take the elevator. If (null = person | person. length <= 2) {return ;}// when the elevator stays on the second floor, stopfloor = 2; n1 = 0; N2 = person [stopfloor]; for (INT I = 3; I <= N; I ++) {N3 + = person [I]; sumfloor + = person [I] * (I-stopfloor );} // when the elevator stays above Layer 3, for (INT I = 3; I <= N; I ++) {If (N1 + N2 <N3) {stopfloor = I; n1 = N1 + N2; N2 = person [I]; N3 = N3-person [I]; sumfloor + = N1 + N2-N3 ;}}} public static void main (string [] ARGs) {targetfloor (person); system. out. println ("staying at:" + stopfloor + "Total number of passenger floors:" + sumfloor );}}

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.