C++ algorithm foundation (3)-elevator time algorithm

When I was visiting Blink today, someone asked a question in a private letter. I think this question is worth learning, so I researched it and recorded it.

1. Title description

There is only one elevator in a building. When you press a number, the elevator will move to that floor. It is known that it takes 6s for the elevator to go up one floor, 4s to go down one floor, and 5s to stay on the floor where it needs to stay. Now, there are several people in the elevator, and everyone has the possibility to go to different floors. Excuse me, how much time did the elevator take after completing the request of the last person?

Insert picture description here

2. Problem-solving ideas

1. First, we need to know how many times the people in the elevator have requested different floors.
2. Which floors are these different requests.
3. Write the function of the time required to go upstairs and downstairs respectively.

int main(){
	int n,sum,i,a[30]={0};
	sum=0;
	cout<<"请输入不同的停留层数:"<<endl;
	scanf("%d",&n);
	cout<<"请输入不同的停留楼层号码:"<<endl;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	for(i=1;i<=n;i++)
	{
		if(a[i]>a[i-1])
		{
			sum=sum+(a[i]-a[i-1])*6+5;
		}
		else
		{
			sum=sum+(a[i-1]-a[i])*4+5;
		}
	}
	cout<<"所需时间:"<<endl;
	printf("%d",sum);
	cout<<"\n"<<endl;
	return 0;
}

Result 1: The elevator stops only one floor
Insert picture description here
Result 2: The elevator stays on two floors and both are upward
Insert picture description here
Result 3: The elevator stops on two floors is downward
Insert picture description here

Result 4: The elevator is both up and down
Insert picture description here

Combined with code analysis: the
core of the code is:

for(i=1;i<=n;i++)
{
	if(a[i]>a[i-1])
	{
		sum=sum+(a[i]-a[i-1])*6+5;
	}
	else
	{
		sum=sum+(a[i-1]-a[i])*4+5;
	}
}

1. First of all, the number of times the elevator rises is related to the number of different floors selected, where n is used to indicate that the elevator has selected several floors.

2. Judge whether the floor is rising or falling. If the elevator is rising all the time, the time consumed by the elevator is the sum of the staying time of each elevator ascending. Assuming that the first time rises by 1 floor, sum = the time to rise one floor, and the second time Ascend 2 levels, then the second time sum = (third level-first level) * time + 5, where the for loop is used to accumulate sum, and the number of levels is represented by a[i] and a[i-1] ( a[i-1] represents the level that was ascended the previous time, a[i] represents the level that was ascended the next time), the expression is: (a[i]-a[i]-1)*time+ 5

3. If the elevator descends, then the time consumed by the elevator is the sum of the staying time of each elevator descending. Assuming that the elevator descends from the third floor to the first floor, then sum = (third floor-first floor) * time + 5, Since the next floor is lower than the previous floor, the expression is: (a[i-1]-a[i])*time+5

Three. Questions

Many people may ask , why can we only use a[i] and a[i-1] to compare floors first? Can we use a[i] and a[i+1] to indicate successive floors?
Here, the answer is that you cannot use a[i] and a[i+1] to indicate successive floors .
why?

In our code, use the following statement to enter the floor.

for(i=1;i<=n;i++)
{
	scanf("%d",&a[i]);
}

Assuming that a[i] and a[i+1] are used to make judgments, what will happen?
When i=1, a[1] is our first input element, assuming it is 1, then a[1]=1, the following will proceed a[1] and a[2] to determine whether the floor is rising or falling , But for a[2] we haven’t entered the element yet, the default is initialized to 0, so it appears, the second element must be smaller than the first element, that is to say, no matter which floor we enter for the second time, the result is 0 Similarly, when i=2, a[2] is the input value, but a[3] is not input, so the default is 0. Changing to for(i=0;i<n;i++) has no effect. , Because when i=0, a[0] is the first element of the input, and a[1] is not input.

Then you can only use a[i] and a[i-1] to define the successive floors. At this time, i must start from 1, because the minimum subscript of the array a starts from 0.
When i=1, a[1] is the first input element, compared with a[0], a[0] has no input, and the default is 0.
When i=2, a[2] is the first input Compared with a[1], a[1] has been input, and you can compare whether the floor is rising or falling.

The details of this article are quite a lot.
Insert picture description here Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/baidu_41191295/article/details/111304496