Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. using namespace std;
  2.  
  3. //Можно добавлять любые вспомогательные методы и классы для решения задачи.
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. class ReadWriter
  13. {
  14. private:
  15.  
  16. std::fstream fin;
  17. std::fstream fout;
  18.  
  19. public:
  20.  
  21. ~ReadWriter()
  22. {
  23. fin.close();
  24. fout.close();
  25. }
  26.  
  27. ReadWriter()
  28. {
  29. fin.open("input.txt", std::ios::in);
  30. fout.open("output.txt", std::ios::out);
  31. }
  32.  
  33. // read 1 int value and empty line
  34. int readInt()
  35. {
  36. if (!fin.is_open())
  37. throw std::ios_base::failure("file not open");
  38.  
  39. int n;
  40. fin >> n;
  41. return n;
  42. }
  43.  
  44. void readVector(vector<int> &vector, int n)
  45. {
  46. if (!fin.is_open())
  47. throw std::ios_base::failure("file not open");
  48.  
  49.  
  50. for (int i = 0; i < n; i++)
  51. fin >> vector[i];
  52. }
  53.  
  54. void writeInt(int a)
  55. {
  56. if (!fout.is_open())
  57. throw std::ios_base::failure("file not open");
  58.  
  59. fout << a << "\n";
  60. }
  61. };
  62.  
  63.  
  64. int solve(int N, vector<int>& upButtons)
  65. {
  66. const int buttonsNum = upButtons.size();
  67. // Посещенность этажей
  68. vector<bool> visited = vector<bool>(N, false);
  69. // Посещаем первый этаж
  70. visited[0] = true;
  71. int visitedFloors = 1;
  72. int newFloorNum;
  73.  
  74. // Запускаем проход по этажам начиная с первого
  75. for(int floorNum = 0; floorNum < N; ++floorNum)
  76. {
  77. // Если этаж посещен - пытаемся посетить с него другие этажи
  78. if(visited[floorNum])
  79. {
  80. // перебираем кнопки
  81. for (int i = 0; i < buttonsNum; ++i)
  82. {
  83. newFloorNum = floorNum + upButtons[i];
  84. // Если этаж не выходит за пределы здания и еще не посещен
  85. if (newFloorNum < N && !visited[newFloorNum])
  86. {
  87. // Посещаем этаж
  88. ++visitedFloors;
  89. visited[newFloorNum] = true;
  90. }
  91. }
  92. }
  93. }
  94.  
  95. return visitedFloors;
  96. }
  97.  
  98.  
  99.  
  100. int main(int argc, const char * argv[])
  101. {
  102. ReadWriter rw;
  103. int N = rw.readInt(); // количество этажей
  104. const int buttonsNum = 3;
  105. vector<int> upButtons = vector<int>(buttonsNum); // кнопки лифта
  106. rw.readVector(upButtons, buttonsNum);
  107.  
  108. //решаем задачу
  109. int adequacy = solve(N, upButtons);
  110.  
  111. //записываем ответ в файл
  112. rw.writeInt(adequacy);
  113.  
  114. return 0;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement