View Code of Problem 49

#include<iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
#pragma warning(disable:4996)


int main() {
	int n;
	int s[100], m[100];
	int max, min;
	max = 0;
	min = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &s[i]);
		m[i] = s[i];
	}
	for (int i = 0; i < n; i++) {
		if (s[i] > s[max]) {
			max = i;
		}
	}
	for (int i = 0; i < n; i++) {
		if (s[i] < s[min]) {
			min = i;
		}
	}
	s[0] = m[min];
	s[min] = m[0];
	s[n - 1] = m[max];
	s[max] = m[n - 1];
	printf("%d", s[0]);
	for (int i = 1; i < n; i++) {
		printf(" %d", s[i]);
	}
	return 0;
}	

Double click to view unformatted code.


Back to problem 49