Advertisement
STANAANDREY

pb6 2/11/2020

Nov 2nd, 2020
2,252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ofstream fout("sport.out");
  5. struct Elev {
  6.     char nume[30];
  7.     float h;
  8. } e[35];
  9. int n, st[35];
  10.  
  11. void read() {
  12.     cin >> n;
  13.     for (int i = 1; i <= n; i++)
  14.         cin >> e[i].nume >> e[i].h;
  15. }
  16.  
  17. void sortare() {
  18.     int sortat = 0;
  19.     while (!sortat) {
  20.         sortat = 1;
  21.         for (int i = 1; i < n; i++)
  22.             if (e[i].h > e[i + 1].h) {
  23.                 sortat = 0;
  24.                 swap(e[i], e[i + 1]);
  25.             }
  26.     }
  27. }
  28.  
  29. void tipar() {
  30.     for (int i = 1; i <= n; i++)
  31.         fout << e[st[i]].nume << ' ';
  32.     fout << endl;
  33. }
  34.  
  35. int valid(int k) {
  36.     if (e[st[k]].h < e[st[k - 1]].h)
  37.         return 0;
  38.     for (int i = 1; i < k; i++)
  39.         if (st[i] == st[k])
  40.             return 0;
  41.     return 1;
  42. }
  43.  
  44. int solutie(int k) {
  45.     return n == k;
  46. }
  47.  
  48. void bktr() {
  49.     int k = 1;
  50.     st[k] = 0;
  51.     while (k) {
  52.         if (st[k] < n) {
  53.             st[k]++;
  54.             if (valid(k)) {
  55.                 if (solutie(k))
  56.                     tipar();
  57.                 else {
  58.                     k++;
  59.                     st[k] = 0;
  60.                 }
  61.             }
  62.         }
  63.         else
  64.             k--;
  65.     }
  66. }
  67.  
  68. int main() {
  69.     read();
  70.     sortare();
  71.     bktr();
  72.     return 0;
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement