View Code of Problem 49

#include<math.h>
#include <iostream>
#include<algorithm>
#include<ctime>
#include<vector>
using namespace std;
typedef long long LL;
 
int main()
{
    int a[15];
    int n;
    while (cin >> n) {
        for (int i = 0;i<n; ++i) {
            cin >> a[i];
        }
        int pmin = 0, pmax = 0;
        for (int i = 1;i<n; ++i) {
            if (a[i] < a[pmin]) {
                pmin = i;
           }
            if (a[i] > a[pmax]) {
                pmax = i;
            }
        }
        
       
        int t = a[0];
        a[0] = a[pmin];
        a[pmin] = t;
        if (pmax == 0)
            pmax = pmin;
        if (n > 0) {
            int t2 = a[n - 1];
            a[n - 1] = a[pmax];
            a[pmax] = t2;
        }
        for (int i = 0;i<n; ++i) {
            if (i)
                putchar(' ');
           
                cout << a[i];
 
        }
        cout << endl;
    }
}

Double click to view unformatted code.


Back to problem 49