The elevator I wrote during the winter vacation was not serious. I thought it was poorly written. After thinking about it, I thought that I should use a structure to hit the elevator. Just the code I wrote during the winter vacation, I was a user and a user. Then calculate the total time.
Not according to the queue given by the teacher < int > destinationQueue; //destination queue int currentFloor; //current position int indicator; //running direction goToFloor() //go to the floor at the head of the queue stop() //stop at the current floor updateDestinationQueue () //Update the destination queue This example writes a constructor and several other functions
C++ is more advanced. I use c to type a lot of changes and I don't know much about it.
And c++ divides the passengers into one object. I think it may be more clear to see the modularization, but in fact, I still feel that it is not deep enough. The special difference is that the code is typed less and more, which may be more understandable.
#include<iostream>
#include <fstream>
using namespace std;
class Elevator {
public:
int destination;
int currentFloor;
Elevator()
{
destination = 0;
currentFloor = 0;
}
int getdestination()
{
return destination;
}
int getcurrentFloor()
{
return currentFloor;
}
void setdestination(int d)
{
destination = d;
}
void setcurrentFloor(int c)
{
currentFloor = c;
}
int time(int d, int b, int t)
{
if (d < b)
t += b - d;
else
t += d - b;
cout << t << " " << b << endl;
return t;
}
int time2(int c, int b, int t)
{
if (b > c)
t += b - c;
else
t += c - b;
cout << t << " " << c << endl;
return t;
}
};
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int n, i, a, b, c, d, f, t = 0;
cin >> n;
Elevator e;
for (i = 0; i < n; i++)
{
cin >> a >> b >> c;
if (a > t)
t = a;
d = e.getcurrentFloor();
if (d != b)
{
f = e.time(d, b, t);
t = f + 1;
e.setcurrentFloor(b);
}
e.setdestination(c);
f = e.time2(c, b, t);
t = f + 1;
e.setcurrentFloor(c);
}
return 0;
}