View Code of Problem 49

#include<stdio.h>
#include<stdlib.h>
void swap(int* a, int* b) {
	if (a == b)return;
	int temp = *a;
	*a = *b;
	*b = temp;
}
int main() {
	int n;
	int* pmax, *pmin;
	scanf("%d", &n);
	int* array = (int*)malloc(sizeof(int)*n);
	for (int i = 0; i<n; ++i) {
		scanf("%d", array + i);
	}
	pmax = pmin = array;
	for (int i = 1; i<n; ++i) {
		if (array[i] < *pmin)pmin = array + i;
		if (array[i] > *pmax)pmax = array + i;
	}
	swap(pmin, array);
	if (pmax != array)
		swap(pmax, array + n - 1);
	for (int i = 0; i<n - 1; ++i) {
		printf("%d ", array[i]);
	}
	printf("%d", array[n - 1]);
	free(array);
	return 0;
}

Double click to view unformatted code.


Back to problem 49