Infiniti_Inter

7. Heap(пирамидальной)(Olya)

Jan 21st, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <fstream>
  4. #include <string>
  5. using namespace std;
  6. ifstream in("input.txt");
  7. ofstream out("output.txt");
  8.  
  9.  
  10.  
  11.  
  12. struct Student
  13. {
  14.     string Name;
  15.     string Surname;
  16.     string SecondName;
  17.     int YearOfBirth;
  18.     int mark[5];
  19.  
  20.     Student() {}
  21.  
  22.     Student(string Surname, string Name, string SecondName, int YearOfBirth, int mark[5])
  23.     {
  24.         this->Name = Name;
  25.         this->Surname = Surname;
  26.         this->SecondName = SecondName;
  27.         this->YearOfBirth = YearOfBirth;
  28.         for (int i = 0; i < 5; ++i)
  29.             this->mark[i] = mark[i];
  30.     }
  31.  
  32.     void InitStudent()//ввод данных (фамилия -> имя -> отчество -> год -> оценки
  33.     {
  34.         in >> Surname >> Name >> SecondName >> YearOfBirth;
  35.         for (int i = 0; i < 5; ++i)
  36.             in >> mark[i];
  37.     }
  38.     int GetSum()
  39.     {
  40.         int sum = 0;
  41.         for (int i = 0; i < 5; ++i)
  42.             sum += mark[i];
  43.         return sum;
  44.  
  45.     }
  46.     void Print()//выовд в файл
  47.     {
  48.         out << "Full name : " << Surname << " " << Name << " " << SecondName << endl;
  49.         out << "Year of birth : " << YearOfBirth << endl;
  50.         out << "Marks : ";
  51.        
  52.         for (int i = 0; i < 5; ++i)
  53.             out << mark[i] << " ";
  54.         out << "\nSum : " << GetSum();
  55.         out << "\n=================================================\n";
  56.     }
  57.  
  58.     bool operator <(Student a)//перегрузка оператора <
  59.     {
  60.         return this->GetSum() > a.GetSum();
  61.     }
  62.     bool operator >(Student a)//перегрузка оператора <
  63.     {
  64.         return this->GetSum() < a.GetSum();
  65.     }
  66.  
  67. };
  68.  
  69. // Функция "просеивания" через кучу - формирование кучи
  70. void siftDown(Student *a, int root, int bottom)
  71. {
  72.     int maxChild; // индекс максимального потомка
  73.     int flag = 0; // флаг того, что куча сформирована
  74.     // Пока не дошли до последнего ряда
  75.     while ((root * 2 <= bottom) && (!flag))
  76.     {
  77.         if (root * 2 == bottom)    // если мы в последнем ряду,
  78.             maxChild = root * 2;    // запоминаем левый потомок
  79.           // иначе запоминаем больший потомок из двух
  80.         else if (a[root * 2] > a[root * 2 + 1])
  81.             maxChild = root * 2;
  82.         else
  83.             maxChild = root * 2 + 1;
  84.         // если элемент вершины меньше максимального потомка
  85.         if (a[root] < a[maxChild])
  86.         {
  87.             swap(a[root], a[maxChild]);
  88.             root = maxChild;
  89.         }
  90.         else // иначе
  91.             flag = 1; // пирамида сформирована
  92.     }
  93. }
  94. // Функция сортировки на куче
  95. void heapSort(Student *a, int size)
  96. {
  97.     // Формируем нижний ряд пирамиды
  98.     for (int i = (size / 2) - 1; i >= 0; i--)
  99.         siftDown(a, i, size - 1);
  100.     // Просеиваем через пирамиду остальные элементы
  101.     for (int i = size - 1; i >= 1; i--)
  102.     {
  103.         swap(a[0], a[i]);
  104.         siftDown(a, 0, i - 1);
  105.     }
  106. }
  107.  
  108.  
  109.  
  110.  
  111. int main()
  112. {
  113.  
  114.     int n; in >> n;
  115.     Student * a = new Student[n];
  116.     for (int i = 0; i < n; ++i)
  117.         a[i].InitStudent();
  118.     heapSort(a, n);
  119.     for (int i = 0; i < n; ++i)
  120.         a[i].Print();
  121.     /*
  122. 11
  123. Bavera Eldred Boreng 2000 1 2 3 4 5
  124. Neoma Boggs Aung 2000 5 5 5 5 5
  125. Ahirlene Bode Feros 2000 3 3 3 3 3
  126. Cong Heston Unofs 2000 5 5 5 3 5
  127. Mario Beane Quri 2000 4 3 2 3 4
  128. Cheryll Balderas Roots 2000 4 2 3 4 2
  129. Shaquana Burkett Gemen 2000 5 5 5 5 4
  130. Nichole Frances Rasif 2000 3 3 3 3 3
  131. Euna Draper Daun 2000 5 5 5 5 5
  132. Carita Henriksen Colers 2000 2 3 4 5 2
  133. Treena Nicholes Samer 2000 4 3 4 5 2
  134.    
  135.     */ 
  136. }
Add Comment
Please, Sign In to add comment