Huawei OD computer test-input an integer array and sorting identifier, and sort its elements in ascending or descending order (C++ & Java & JS & Python)

describe

Input an integer array and a sort identifier to sort its elements in ascending or descending order

Data range: 1≤�≤1000 1≤n≤1000, the element size satisfies 0≤���≤100000 0≤val≤100000 

Enter description:

The first line enters the number of array elements
. The second line enters the array to be sorted. Each number is separated by a space. The
third line enters an integer 0 or 1. 0 represents ascending order and 1 represents descending order.

Output description:

Output sorted numbers

Example 1

enter:

8
1 2 4 9 3 55 64 25
0

Output:

1 2 3 4 9 25 55 64

Example 2

enter:

5
1 2 3 4 5
1

Output:

5 4 3 2 1

Java:

import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int n = sc.nextInt();//接收数组长度
			int[] arr = new int[n];//创建数组

			for (int i = 0; i < n; i++) {//数组填入
				arr[i] = sc.nextInt();
			}
			
			int flag = sc.nextInt();//接收排序标识
			Arrays.sort(arr);//数组排序

			if (

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/132895813