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 = str[0];
	max = str[n-1];
	int tmp1 = 0;
	int tmp2 = 0;
	for (int i=0; i < n; i++) {
		if (str[i] < min) {
			tmp1 = i;
			min = str[i];
		}
	}
	for (int i=0; i < n; i++) {
		if (str[i] > max) {
			tmp2 = i;
			max = str[i];
		}
	}
	out[0] = min;
	out[n - 1] = max;
	out[tmp1] = str[0];
	out[tmp2] = str[n - 1];
	for (int i = 0; i < n; i++) {
		cout << out[i]<<" ";
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 49