/
ArtT
/
Netology
Обзор
Документация
Войти
/
ArtT
/
Netology
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Part2/Task_2_15_2/Task_2_15_2.cpp
101 строка
3 KB
ArtT
Добавил пятнадцатое задание
09 фев 2026, 15:49
09 фев 2026, 15:49
160addf
Код
Авторство
О чём код?
#include <iostream> #include <string> #include <cmath> #include <Windows.h> std::string str_array(int* arr, int size) { std::string s = ""; for (int i = 0; i < size; i++) { s += std::to_string(arr[i]) + " "; } return s; } std::string print_pyramid(int* arr, int size) { int n{ 0 }; std::string s = "\n"; for (int i = 0; i < size; i++) { n = floor(log2(i + 1)); if (i == 0) s += std::to_string(n) + " " + "root " + std::to_string(arr[i]) + "\n"; else if (i % 2 == 1) { s += std::to_string(n) + " " + "left(" + std::to_string(arr[(i - 1) / 2]) + ") " + std::to_string(arr[i]) + "\n"; } else { s += std::to_string(n) + " " + "right(" + std::to_string(arr[(i - 1) / 2]) + ") " + std::to_string(arr[i]) + "\n"; } } return s; } int get_parent_index(int* arr, int cur_index) { return (cur_index - 1) / 2; } void from_index(int* arr, int cur_index) { if (cur_index == 0) std::cout << "Вы находитесь здесь: 0 root " << arr[0] << std::endl; else { int n = floor(log2(cur_index + 1)); std::cout << "Вы находитесь здесь: " << n << (cur_index % 2 == 1 ? " left(" : " right(") << arr[get_parent_index(arr, cur_index)] << ") " << arr[cur_index] << std::endl; } } bool left_index(int* arr, int size, int& cur_index) { if (cur_index * 2 + 1 > size-1) return false; cur_index = cur_index * 2 + 1; return true; } bool right_index(int* arr, int size, int& cur_index) { if (cur_index * 2 + 2 > size-1) return false; cur_index = cur_index * 2 + 2; return true; } bool set_parent_index(int* arr, int& cur_index) { if (cur_index == 0) return false; cur_index = (cur_index - 1) / 2; return true; } int main() { SetConsoleCP(1251); SetConsoleOutputCP(1251); int arr[]{ 1, 3, 6, 5, 9, 8 }; //int arr[]{ 94, 67, 18, 44, 55, 12, 6, 42 }; //int arr[]{ 16, 11, 9, 10, 5, 6, 8, 1, 2, 4 }; const int sizeArr = sizeof(arr) / sizeof(arr[0]); std::cout << "Исходный массив: " << str_array(arr, sizeArr) << std::endl; std::cout << "Пирамида: " << print_pyramid(arr, sizeArr) << std::endl; int cur_index{ 0 }; int n{ 0 }; std::string inputValue = ""; from_index(arr, cur_index); do { std::cout << "Введите команду: "; std::getline(std::cin, inputValue); if (inputValue == "left"){ if (left_index(arr, sizeArr, cur_index)) std::cout << "Ок" << std::endl; else std::cout << "Ошибка! Отсутствует левый потомок" << std::endl; } else if (inputValue == "right") { if (right_index(arr, sizeArr, cur_index)) std::cout << "Ок" << std::endl; else std::cout << "Ошибка! Отсутствует правый потомок" << std::endl; } else if (inputValue == "up") { if (set_parent_index(arr, cur_index)) std::cout << "Ок" << std::endl; else std::cout << "Ошибка! Отсутствует родитель" << std::endl; } from_index(arr, cur_index); } while (inputValue != "exit"); return EXIT_SUCCESS; }