Advertisement
ioana_martin98

Untitled

Mar 27th, 2022
1,031
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int v[1001], aux, n, i, j, max, min, pozMax = 0, pozMin = 0;
  7.     cin >> n;
  8.     min = 10000000;
  9.     max = -1000000;
  10.  
  11.     for (i = 1; i <= n; i++)
  12.     {
  13.         cin >> v[i];
  14.         if (v[i] > max)
  15.         {
  16.             pozMax = i;
  17.             max = v[i];
  18.         }
  19.         if (v[i] < min)
  20.         {
  21.             pozMin = i;
  22.             min = v[i];
  23.         }
  24.     }
  25.  
  26.     if (pozMin < pozMax)
  27.     {
  28.         for (i = pozMin; i < pozMax; i++)
  29.         {
  30.             for (j = i + 1; j <= pozMax; j++)
  31.             {
  32.                 if (v[i] > v[j])
  33.                 {
  34.                     aux = v[i];
  35.                     v[i] = v[j];
  36.                     v[j] = aux;
  37.                 }
  38.             }
  39.         }
  40.     }
  41.     else
  42.     {
  43.         for (i = pozMax; i < pozMin; i++)
  44.         {
  45.             for (j = i + 1; j <= pozMin; j++)
  46.             {
  47.                 if (v[i] > v[j])
  48.                 {
  49.                     aux = v[i];
  50.                     v[i] = v[j];
  51.                     v[j] = aux;
  52.                 }
  53.             }
  54.  
  55.         }
  56.     }
  57.  
  58.     for (i = 1; i <= n; i++)
  59.         cout << v[i] << " ";
  60.  
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement