Advertisement
Ifrail

Task 7

Nov 13th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.96 KB | None | 0 0
  1. /*Летим в другой сектор*/
  2.  
  3. /*
  4. День 27.
  5. Поздравляю старший кадет %username%, ваша подготовка закончена и теперь вы капитан! Сегодня вам будут предоставлены настоящий корабль и настоящий И.С.А.А.К. Ваша задача добраться до системы Процион, по заданным вам координатам. На вашем курсе будут 2 заправочные станции, так что воспользуйтесь ими при необходимости. Как и раньше лучшим результатом считается минимальное количество сделанных вами дозаправок, удачи капитан. Используйте весь накопленный опыт для решения этой задачи и будьте осторожны с кефиром на борту!
  6.  
  7. Гарантируется, что дальняя от вас станция ближе к конечной цели. Сначала вводятся координаты ближней станции, потом дальней.
  8. */
  9.  
  10. #include <iostream>
  11. #include <string>
  12. #include <cmath>
  13.  
  14. using namespace std;
  15.  
  16. class SpaceShip {
  17. public:
  18.     // Остаток топлива
  19.     int fuelLeft;
  20.     // Вместимость топливного бака (максимум топлива)
  21.     int fuelMax;
  22.     // Название корабля
  23.     string name;
  24.     // Количество дозаправок
  25.     int fillCount = 0;
  26.     // Потерялся ли корабль в космосе
  27.     bool isLost = false;
  28.     //Координаты корабля
  29.     int X, Y;
  30.  
  31.     void printStatus() {
  32.         cout << fuelLeft << " fuel left from " << fuelMax << " at the ship " << name << endl;
  33.         cout << "Status: ";
  34.         if (isLost)
  35.             cout << "lost in space";
  36.         else
  37.             cout << "target reached with " << fillCount << " fill up";
  38.     }
  39.  
  40.     int calculateDistance(int x, int y) {
  41.         return sqrt((X - x)*(X - x) + (Y - y)*(Y - y));
  42.     }
  43.  
  44.     void flight(int x, int y) {
  45.         int distance = calculateDistance(x, y);
  46.         fuelLeft -= distance;
  47.         if (fuelLeft < 0) {
  48.             fuelLeft = 0;
  49.             isLost = true;
  50.         }
  51.         X = x;
  52.         Y = y;
  53.     }
  54.  
  55.  
  56.     void prepare() {
  57.         cin >> name >> fuelLeft >> fuelMax >> X >> Y;
  58.     }
  59. };
  60.  
  61. class SpaceStation {
  62. public:
  63.     // Название станции
  64.     string name;
  65.     // Расстояние до станции
  66.     int X, Y;
  67.  
  68.     void prepare() {
  69.         cin >> name >> X >> Y;
  70.     }
  71.  
  72.     void fillupShip(SpaceShip &ship) {
  73.         int fillCount = ship.fuelMax - ship.fuelLeft;
  74.         ship.fuelLeft = ship.fuelMax;
  75.         ship.fillCount++;
  76.         cout << "Ship " << ship.name << " filled " << fillCount << " tn fuel on station " << name << endl;
  77.     }
  78.  
  79. };
  80.  
  81. int main() {
  82.  
  83.     SpaceShip space_ship;
  84.     SpaceStation space_station1, space_station2;
  85.  
  86.     space_ship.prepare();
  87.     space_station1.prepare();
  88.     space_station2.prepare();
  89.  
  90.     int finallyX, finallyY;
  91.     cin >> finallyX >> finallyY;
  92.  
  93.  
  94.     //Проверяем долетим ли до конечной цели
  95.     int dist = space_ship.calculateDistance(finallyX, finallyY);
  96.     if (dist > space_ship.fuelLeft) {
  97.         // Проверяем дальнюю станцию
  98.         dist = space_ship.calculateDistance(space_station2.X, space_station2.Y);
  99.         if (dist <= space_ship.fuelLeft) {
  100.             space_ship.flight(space_station2.X, space_station2.Y);
  101.             space_station2.fillupShip(space_ship);
  102.             space_ship.flight(finallyX, finallyY);
  103.         } else {
  104.             // Летим на ближнюю
  105.             space_ship.flight(space_station1.X, space_station1.Y);
  106.             if (!space_ship.isLost) {
  107.                 // Долетели, проверяем конечную цель
  108.                 space_station1.fillupShip(space_ship);
  109.                 dist = space_ship.calculateDistance(finallyX, finallyY);
  110.                 if (dist > space_ship.fuelLeft) {
  111.                     // Летим на вторую станцию
  112.                     space_ship.flight(space_station2.X, space_station2.Y);
  113.                     if (!space_ship.isLost) {
  114.                         space_station2.fillupShip(space_ship);
  115.                         space_ship.flight(finallyX, finallyY);
  116.                     }
  117.                 }
  118.                 else space_ship.flight(finallyX, finallyY);
  119.             }
  120.         }
  121.     } else space_ship.flight(finallyX, finallyY);
  122.  
  123.     space_ship.printStatus();
  124.  
  125.     return 0;
  126. }
  127. /*
  128. Тест1
  129. Ввод:
  130. Firefly 24 24 0 0
  131. Omicron-1 6 6
  132. Omicron-2 12 12
  133. 32 25
  134. Вывод:
  135. Ship Firefly filled 16 tn fuel on station Omicron-2
  136. 1 fuel left from 24 at the ship Firefly
  137. Status: target reached with 1 fill up
  138.  
  139. Тест2
  140. Ввод:
  141. Enterprise 100 100 0 0
  142. Sunrise 50 30
  143. MoonRise 90 60
  144. 120 110
  145. Вывод:
  146. Ship Enterprise filled 58 tn fuel on station Sunrise
  147. Ship Enterprise filled 50 tn fuel on station MoonRise
  148. 42 fuel left from 100 at the ship Enterprise
  149. Status: target reached with 2 fill up
  150.  
  151. Тест3
  152. Ввод:
  153. Nostromo 40 50 0 0
  154. LV-426 20 30
  155. MoonRise 70 70
  156. 120 110
  157. Вывод:
  158. Ship Nostromo filled 46 tn fuel on station LV-426
  159. 0 fuel left from 50 at the ship Nostromo
  160. Status: lost in space
  161. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement