Title Requirements:
Shijiazhuang Railway University Foundation building a total of four elevators, each floor are people up and down, elevators in each floor stop. Zhang Yidong of class 1201-1 felt impatient at the stop on each floor.
Because the floor is not too high, at the peak of the Class Period, the elevator from the first floor, but only allowed to stop on a certain floor. On the first floor, each passenger chooses his or her destination, and the elevator automatically calculates the floor that should be stopped.
Ask the elevator to stop at that floor, to ensure that all passengers who take the elevator climb the stairs of the minimum number of floors.
First, the solution of ideas
for an I layer, suppose that the array is smaller than the I layer has BLOWC, is equal to the I layer has EQULC, is greater than the I layer of HIGHC. Need to set A to I layer to go all the floor and the absolute value array, to record the sum of the number of layers to be stopped on each floor , less than i+1 of the BLOWC+EQULC layer, greater than i+1 have BLOWC-EQULC layer, compared to Y (i) , the absolute value of the difference above the I layer and below the I layer is increased, and the BLOWC+EQULC is reduced by BLOWC-EQULC. Find out the total number of stairs climbed in each floor, and choose the least stop.
Second, the program code
1#include"stdafx.h"2#include <iostream.h>3#include"stdlib.h"4 #defineMax 10005 voidFloornum (int*inch,intnum)6 {7 intresult[max]={0};8 intBLOWC,EQULC,HIGHC;//indicate less than, equal to, and greater than number of I9 intCur=0, I;Ten intb=0, h=0; OneBlowc=0; AHighc=num; -Equlc=0; - for(i=0; i<num;i++){ theH=inch[i]+h; - } -result[0] =h; -cout<<"The sum of stairs that were climbed on each floor was:"<<Endl; + for(i=1; i<=inch[num-1];i++){ -blowc=equlc+BLOWC; +highc=equlc-HIGHC; Ah=highc-h; atb=blowc+h; -result[i]=b+h; - { -cout<<"stop at"<<i<<"Layer"<<"the total number of stairs climbed:"<<result[i]<<Endl; - } -Equlc=0; in while(inch[cur]==i) { -cur++; toEqulc++; + } - } the } * intMainintargcChar*argv[]) $ {Panax Notoginseng intNperson; - int inch[Max]; thecout<<"number of people entering elevators:"<<Endl; +Cin>>Nperson; Acout<<"Please enter the number of floors that each person presses after entering the elevator."<<Endl; the for(intI=0; i<nperson;i++) + { -Cin>>inch[i]; $ } $Floornum (inch, Nperson); - return 0; - the}
Iii. Results
Iv. Experience
Class, the beginning is very tangled why to put a good elevator design so that in real life can not be applied, and later figured out, in fact, this is an algorithm, an optimization design, and then under the guidance of the teacher, I take it as a math problem to do. The simplest and dumbest solution to consider first is to enumerate the X-nth layer from the first layer and then calculate how many floors all passengers will have to climb if the elevator stops at Layer x. This program code is a two-cycle, but the efficiency is too low, time complexity is high, later after the teacher woke up, using the results of the previous calculations, is the maximum use of every operation, the time complexity from O (n^2) to O (N). the algorithm is optimized to achieve the purpose of program design.
Follow-up practice--Elevator scheduling algorithm