Advertisement
Shokedbrain

Тимус 1319

Dec 1st, 2021
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.   int N;
  6.   std:: cin >> N;
  7.   /* dynamic memory allocation */
  8.   int ** p = new int * [N];
  9.   for (int i = 0; i < N; i++)
  10.     p[i] = new int [N];
  11.  
  12.   /* initialize upper triangle matrix */
  13.   p[0][N-1] = 1;
  14.   int m, n;
  15.   int ct = 1, value = 2, iter = 1, row = 0, column = N - 2;
  16.   while (ct <= N - 1)
  17.   {
  18.     n = column, m = row;
  19.     for (int i = 0; i <= iter; i++)
  20.     {
  21.       p[m++][n++] = value;
  22.       value++;
  23.     }
  24.  
  25.     iter++; ct++;
  26.     column--;
  27.   }
  28.  
  29.   /* initialize the lower triangle matrix */
  30.   row = 1; column = 0, iter = N - 2;
  31.   while (ct > 1)
  32.   {
  33.     n = column, m = row;
  34.     for (int i = 0; i <= iter; i++)
  35.     {
  36.       p[m++][n++] = value;
  37.       value++;
  38.     }
  39.  
  40.     iter--; ct--;
  41.     row++;
  42.   }
  43.  
  44.   /* display matrix */
  45.   for (int i = 0; i < N; i++)
  46.   {
  47.     for (int j = 0; j < N; j++)
  48.       std::cout << p[i][j] << " ";
  49.     std::cout << std::endl;
  50.   }
  51.  
  52.   /* release memory */
  53.   for (int i = 0; i < N; i++)
  54.     delete[] p[i];
  55.   delete[] p;
  56.  
  57.   return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement