allia

дерево,,,

Dec 2nd, 2020 (edited)
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.  int n;
  9.  cin >> n;
  10.  
  11.  int *arr = new int[n-1];
  12.  int max = -1;
  13.  
  14.  for (int i = 0; i < n-1; i++)
  15.    {
  16.      cin >> arr[i];
  17.      if (arr[i] > max)
  18.      max = arr[i];
  19.    }
  20.  
  21. int **tree = new int*[max];
  22. for (int i = 0; i < n; i++)
  23.    tree[i] = new int[n];
  24.  
  25. tree[0][0] = 0;
  26.  
  27. for (int i = 0; i < max; i++)
  28.  for (int j = 0; j < n-1; j++)
  29.   {
  30.     if (arr[j] == i+1)
  31.      tree[i][j+1] = 1;
  32.     else tree[i][j+1] = 0;
  33.   }
  34.  
  35. for (int i=0; i<max; i++)
  36.     {
  37.       for (int j=0; j<n; j++)
  38.       {
  39.         cout.width(3);
  40.         cout << tree[i][j] << " ";
  41.       }
  42.      cout << endl;
  43.     }
  44.  
  45. cout << '1' << " ";
  46. for (int i = 0; i < max; i++)
  47.  for (int j = 0; j < n; j++)
  48.   {
  49.     if (i%2 != 0 && tree[i][j] == 1)
  50.      cout << j+1 << " ";
  51.     if (i%2 == 0 && tree[i][n - j -1] == 1)
  52.      cout << n-j << " ";
  53.   }
  54. }
Add Comment
Please, Sign In to add comment