First, design ideas
(1) Save the number of floors the passengers are going to.
(2) Assuming that Yi is the passenger to climb the number of floors of the sum, yi=n1*| (N1-ni) |+n2*| (N2-ni) |+. +n18*| (N18-ni) |
(3) Compare the values of Y1 to Y18 to find the smallest.
(4) Output results
Second, the source code
#include "iostream.h" #include "stdlib.h" #define MAXSIZE 15void input (int &num,int flour[])//Enter the number of people travelling by elevator and Floor {cout<< "************** Elevator scheduling applet ***************" <<endl; cout<< "Please enter the number of people who take the elevator (limited to 15 persons):"; cin>>num;cout<<endl; if (num>15) {cout<< "Input error! "<<endl; Exit (0); } cout<< "Please enter the number of layers each person is going to (up to 18 storeys):"; for (int i=0;i<num;i++) {cin>>flour[i]; if (flour[i]>18) {cout<< "Input error! "<<endl; Exit (0); }}}void getsmallestflour (int num,int flour[],int &small)//calculates the floor {int Sum,temp=small with the lowest number of stairs to climb; int highest,lowest; HIGHEST=LOWEST=FLOUR[0]; for (int i=0;i<num;i++)//Find the highest floor in the floor to go {if (flour[i]>highest) {highest=f Lour[i]; } else//find the lowest floor {lowest=flour[i]; }} for (i=lowest;i<=highest;i++)//from the lowest floorThe layer begins to traverse until the highest floor {sum=0; for (int j=0;j<num;j++) {if ((flour[j]-i) <0)//Calculates the sum of the number of floors of the stair ladder {su M=SUM+I-FLOUR[J]; } else {sum=sum+flour[j]-i; }}cout<<endl; cout<< "When the elevator stops at the" <<i<< "floor, the passenger climbs the stairs with the sum of" <<sum<<endl;cout<<endl; if (sum<temp) {small=i; Record and the smallest floor number temp=sum; Record the smallest and}}}int main (int argc, char* argv[]) {int num,aver,flour[maxsize]; Input (Num,flour); aver=0; for (int i=0;i<num;i++) {aver+=flour[i]; } getsmallestflour (Num,flour,aver); cout<< "The elevator will stop at the <<aver<<" floor. "<<endl;cout<<endl; return 0;}
Iii. Results of operation
Iv. Experience
Although I think this elevator algorithm is not human-friendly, but it is the most powerful solution to the problem of clogging. The main problem is that logic starts a little messy.
Elevator dispatching--c++--Software engineering