새로새록
[c++]8. 기차의 사람 수 계산 본문
Ktx 열차가 출발역, 종착역을 포함하여 총 5개의 역에 정차한다. 이 때 각 역에서는 입력된 숫자만큼의 사람이 내리거나 탑승한다. 이 기차는 아래와 같은 조건을 만족하면서 운행된다고 가정한다.
1. 기차는 역 번호 순서대로 운행한다.
2. 출발역에서 내린 사람 수와 종착역에서 탄 사람 수는 0이다.
3. 각 역에서 현재 기차에 있는 사람보다 더 많은 사람이 내리는 경우는 없다.
4. 기차의 정원은 최대 300명이고, 정원을 초과하여 타는 경우는 없다.
5개의 역에 대해 기차에서 내린 사람 수와 탄 사람 수가 주어졌을 때, 기차에 사람이 가장 많을 때의 사람 수를 계산하는 프로그램을 작성하라.
샘플코드
int main()
{
Ktx k;
// ???
return 0;
}
[참조 1]
class Train {
public:
Train() {}
Train(int people)
{
mPeople = people;
}
~Train() {}
virtual int station(int takeOff, int takeOn);
protected:
int mPeople; // 사람 수
};
[참조 2]
class Ktx : public Train
{
public:
Ktx() : Train(0) {}
Ktx(int people) : Train(people)
{}
~Ktx() {}
// 기차에 사람이 타고 내리는 함수
int station(int takeOff, int takeOn);
int getPeople();
};
#include <iostream>
using namespace std;
class Train {
public:
Train() {}
Train(int people) { mPeople = people; }
~Train() {}
virtual int station(int takeOff, int takeOn) {
mPeople = mPeople - takeOff + takeOn;
return mPeople;
}
protected:
int mPeople=0;
};
class Ktx : public Train {
public:
Ktx() : Train(0) {}
Ktx(int people) : Train(people) {}
~Ktx() {}
int station(int takeOff, int takeOn) override{
return Train::station(takeOff, takeOn);
}
int getPeople() { return mPeople; }
};
int main() {
int max = 0;
Ktx k;
int i = 1;
while (i <= 5) {
while (1) {
int off, on;
cout << i << "번역 : ";
cin >> off >> on;
if (k.getPeople() - off < 0) {
cout << "정원미달입니다" << endl;
continue;
}
else if (k.getPeople() - off + on > 300) {
cout << "정원초과입니다" << endl;
continue;
}
else {
k.station(off, on);
if (k.getPeople() > max) max = k.getPeople();
break;
}
}
i++;
}
cout << "가장 많은 사람이 탑승 했을 때의 사람 수 = " << max << endl;
return 0;
}
'소프트웨어융합 > 경희대 c++ 과제' 카테고리의 다른 글
[c++]9. 학생과 조교 (0) | 2020.07.16 |
---|---|
[c++]8. 어벤져스 캐릭터 배틀 (0) | 2020.07.16 |
[c++]8. 상속받는 삼각형, 사각형, 원 (0) | 2020.07.16 |
[c++]7. 로그인 정보 연속 받기 (0) | 2020.07.16 |
[c++]7. 계좌 정리 (0) | 2020.07.16 |