View Code of Problem 49

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


int main() {
	int n;
	int s[100];
	int max, min;
	max = 0;
	min = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &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;
		}
	}

	swap(s[0], s[min]);
	swap(s[n - 1], s[max]);
	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