View Code of Problem 49

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;

int main() {
	int n;
	cin >> n;
	int str[100],out[100];
	int max, min;
	for (int i=0; i < n; i++) {
		cin >> str[i];
		out[i] = str[i];
	};
	min = 0;
	max = 0;
	for (int i=0; i < n; i++) {
		if (str[i] < str[min]) {
			min = i;
		}
	}
	for (int i=0; i < n; i++) {
		if (str[i] > str[max]) {
			max=i;
		}
	}
	out[0] = str[min];
	out[min]=str[0];
	out[n-1] = str[max];
	out[max]=str[n-1];
	cout << out[0];
	for (int i = 1; i < n; i++) {
		cout <<" "<< out[i];
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 49