import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] nums = new int[n]; sc.nextLine(); String input = sc.nextLine(); String[] line = input.split(" "); for(int i = 0; i < n; i ++) { nums[i] = Integer.parseInt(line[i]); } int max_index, min_index; max_index = min_index = 0; for(int i = 1; i < n; i ++) { if(nums[max_index] < nums[i]) { max_index = i; } if(nums[min_index] > nums[i]) { min_index = i; } } int temp; temp = nums[min_index]; nums[min_index] = nums[0]; nums[0] = temp; temp = nums[max_index]; nums[max_index] = nums[nums.length - 1]; nums[nums.length - 1] = temp; for(int i = 0; i < n; i ++) { System.out.print(nums[i]); if(i < n - 1) { System.out.print(" "); } } } } |
Double click to view unformatted code.