Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- //Можно добавлять любые вспомогательные методы и классы для решения задачи.
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- using namespace std;
- class ReadWriter
- {
- private:
- std::fstream fin;
- std::fstream fout;
- public:
- ~ReadWriter()
- {
- fin.close();
- fout.close();
- }
- ReadWriter()
- {
- fin.open("input.txt", std::ios::in);
- fout.open("output.txt", std::ios::out);
- }
- // read 1 int value and empty line
- int readInt()
- {
- if (!fin.is_open())
- throw std::ios_base::failure("file not open");
- int n;
- fin >> n;
- return n;
- }
- void readVector(vector<int> &vector, int n)
- {
- if (!fin.is_open())
- throw std::ios_base::failure("file not open");
- for (int i = 0; i < n; i++)
- fin >> vector[i];
- }
- void writeInt(int a)
- {
- if (!fout.is_open())
- throw std::ios_base::failure("file not open");
- fout << a << "\n";
- }
- };
- int solve(int N, vector<int>& upButtons)
- {
- const int buttonsNum = upButtons.size();
- // Посещенность этажей
- vector<bool> visited = vector<bool>(N, false);
- // Посещаем первый этаж
- visited[0] = true;
- int visitedFloors = 1;
- int newFloorNum;
- // Запускаем проход по этажам начиная с первого
- for(int floorNum = 0; floorNum < N; ++floorNum)
- {
- // Если этаж посещен - пытаемся посетить с него другие этажи
- if(visited[floorNum])
- {
- // перебираем кнопки
- for (int i = 0; i < buttonsNum; ++i)
- {
- newFloorNum = floorNum + upButtons[i];
- // Если этаж не выходит за пределы здания и еще не посещен
- if (newFloorNum < N && !visited[newFloorNum])
- {
- // Посещаем этаж
- ++visitedFloors;
- visited[newFloorNum] = true;
- }
- }
- }
- }
- return visitedFloors;
- }
- int main(int argc, const char * argv[])
- {
- ReadWriter rw;
- int N = rw.readInt(); // количество этажей
- const int buttonsNum = 3;
- vector<int> upButtons = vector<int>(buttonsNum); // кнопки лифта
- rw.readVector(upButtons, buttonsNum);
- //решаем задачу
- int adequacy = solve(N, upButtons);
- //записываем ответ в файл
- rw.writeInt(adequacy);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement