首页 > 试题广场 >

输入整型数组和排序标识,对其元素按照升序或降序进行排序

[编程题]输入整型数组和排序标识,对其元素按照升序或降序进行排序
  • 热度指数:214031 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
输入整型数组和排序标识,对其元素按照升序或降序进行排序

数据范围: ,元素大小满足

输入描述:

第一行输入数组元素个数
第二行输入待排序的数组,每个数用空格隔开
第三行输入一个整数0或1。0代表升序排序,1代表降序排序



输出描述:

输出排好序的数字

示例1

输入

8
1 2 4 9 3 55 64 25
0

输出

1 2 3 4 9 25 55 64
示例2

输入

5
1 2 3 4 5
1

输出

5 4 3 2 1
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        Integer[] arr = new Integer[a];
        for(int i = 0; i < a; i++){
            arr[i] = in.nextInt();
        }
        int b = in.nextInt();
        if(b == 0){
            Arrays.sort(arr);
        }else{
            Arrays.sort(arr, Collections.reverseOrder());
        }
        for(int i = 0;i < arr.length; i++){
            System.out.print(arr[i] + " ");
        }
    }
}
编辑于 2024-03-19 16:09:34 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Integer> r = new ArrayList();
        for (int i = 0; i < n; ++ i) {
            r.add(in.nextInt());
        }
        Collections.sort(r);
        if (in.nextInt() == 1) Collections.reverse(r);
        r.forEach(item -> { System.out.printf("%d ", item);});
    }
}

编辑于 2024-03-16 16:40:55 回复(0)
import java.util.*;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int numSize = in.nextInt();
        List<Integer> nums = new ArrayList<>();
        for (int i = 0; i < numSize; i++) {
            nums.add(in.nextInt());
        }
        List<Integer> list = nums.stream().sorted().collect(Collectors.toList());
        int lastInt = in.nextInt();
        if (lastInt == 0) {
            for (int i = 0; i < list.size(); i++) {
                System.out.print(list.get(i) + " ");
            }
        } else {
            for (int i = list.size() - 1; i >= 0; i--) {
                System.out.print(list.get(i) + " ");
            }
        }
    }
}

编辑于 2024-02-20 11:07:24 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int k = in.nextInt();
        Integer[] nums = new Integer[k];
        for (int i = 0; i < k; i++) {
            nums[i] = in.nextInt();
        }
        if (in.nextInt() == 0) {
            Arrays.sort(nums);
        } else {
            Arrays.sort(nums, (a, b)->(b-a));
        }
        for (int num : nums) {
            System.out.print(num + " ");
        }
    }
}

发表于 2023-12-07 23:07:37 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        List<Integer> list = new ArrayList();
        for(int i = 0; i < n; i++){
            list.add(in.nextInt());
        }
        int t = in.nextInt();
        Collections.sort(list, (o1, o2)-> t == 0 ? o1 - o2: o2 - o1);
        list.forEach(k -> System.out.print(k + " "));
    }
}

发表于 2023-11-28 20:56:38 回复(0)
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();

        ArrayList<Integer> list = new ArrayList<>();
        String arrstr = sc.nextLine();
        String[] split = arrstr.split(" ");
        for (int i = 0; i < split.length; i++) {
            int num = Integer.parseInt(split[i]);
            list.add(num);
        }

        String sss = sc.nextLine();
        int i = Integer.parseInt(sss);
        if (i == 0) {
            sx(list);
            bl(list);
        } else {
            jx(list);
            bl(list);
        }

    }

    //升序方法
    public static void sx(ArrayList<Integer> list) {
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1 - o2;
            }
        });
    }


    //降序方法
    public static void jx(ArrayList<Integer> list) {
        list.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
    }


    //遍历list
    public static void bl(ArrayList<Integer> list) {
        Iterator<Integer> it = list.iterator();
        while (it.hasNext()) {
            Integer next = it.next();
            System.out.print(next + " ");
        }
    }
}

发表于 2023-11-17 16:39:03 回复(0)
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int totalNum = sc.nextInt();
        int[] source = new int[totalNum];
        for (int i = 0; i < totalNum; i++) {
            source[i] = sc.nextInt();
        }
        boolean orderFlag = 0 == sc.nextInt();
        TreeSet<Integer> integers = new TreeSet<>((pre, current) -> {
            int diff = pre - current;
            diff = orderFlag ? diff : -diff;
            return diff == 0 ? 1 : diff;
        });
        for (int i : source) {
            integers.add(i);
        }
        StringBuilder result = new StringBuilder();
        for (Integer integer : integers) {
            result.append(" ").append(integer);
        }
        System.out.println(result.delete(0, 1));
    }
}

发表于 2023-08-10 15:15:06 回复(1)
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            int[] array = new int[a];
            for (int i = 0; i < a; i++) {
                array[i] = in.nextInt();
            }
            int b = in.nextInt();
            if (b == 0) {
                Arrays.sort(array);
                for (int y = 0; y < a; y++) {
                    System.out.print(array[y] + " ");
                }
            } else {
                for (int u = 0; u < a; u++) {
                    for (int i = 0; i < a - 1; i++) {
                        if (array[i] < array[i + 1]) {
                            int num = array[i + 1];
                            array[i + 1] = array[i];
                            array[i] = num;
                        }
                    }

                }
                for (int y = 0; y < a; y++) {
                    System.out.print(array[y] + " ");
                }
            }
        }
    }
}

发表于 2023-07-10 09:38:44 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String s1 = in.nextLine();
            String str = in.nextLine();
            String s = in.nextLine();
            String[] s2 = str.split(" ");
            int arr[] = new int[s2.length];
            for (int i = 0; i < s2.length; i++) {
                arr[i] = Integer.parseInt(s2[i]);
            }
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length - 1 - i; j++) {
                    int temp = arr[j];
                    if (arr[j] > arr[j + 1]) {
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            if (Integer.parseInt(s) == 0) {
                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[i] + " ");
                }
                System.out.println();
            } else if (Integer.parseInt(s) == 1) {
                for (int i = 0; i < arr.length; i++) {
                    System.out.print(arr[arr.length - 1 - i] + " ");
                }
                System.out.println();
            }
        }

    }
}
发表于 2023-07-09 20:40:03 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int a = in.nextInt();
        Integer[] arr = new Integer[a];
        for(int i = 0;i< a ;i++){
            int c = in.nextInt();
            arr[i] = c;
        }
        int b = in.nextInt();
        if(b == 0){
            Arrays.sort(arr);
        }else{
            Arrays.sort(arr,Collections.reverseOrder());
        }
        for(int i : arr){
            System.out.print(i+" ");
        }
    }
}

发表于 2023-06-08 17:41:41 回复(0)
这个题主要是考察排序 会写个排序算法即可 当然也可以用 Arrays这个类里面的sort排序
关于输出 0是正常遍历 1是反向遍历
发表于 2023-05-12 06:30:45 回复(0)

简单粗暴

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        while (input.hasNext()) {
            int length = input.nextInt();
            int[] datas = new int[length];
            for (int i = 0; i < length; i++) {
                datas[i] = input.nextInt();
            }
            int order = input.nextInt();
            arrayOrder(length, order, datas);
        }
    }

    private static void arrayOrder(int length, int order, int[] datas) {
        List<Integer> arrayList = new ArrayList<>(length);
        for (int data : datas) {
            arrayList.add(data);
        }
        if (order == 0) {
            // 升序
            Collections.sort(arrayList);
        } else {
            // 降序
          arrayList.sort((element1, element2) -> element1 > element2 ? -1 : 1 );
        }
        arrayList.forEach(element -> System.out.print(element + " "));
        System.out.println();
    }
}
发表于 2023-05-06 08:19:53 回复(0)
 Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int nn = in.nextInt();
            // String str = in.next();
            List<Integer> list = new ArrayList<Integer>();
            for(int i=0;i<nn;i++){
                int num = in.nextInt();
                list.add(num);
            }
            int sortType = in.nextInt();
            if(sortType==0){
                list.sort(new Comparator<Integer>(){
                    @Override
                    public int compare(Integer int1,Integer int2){
                        return int1-int2;
                    }
                });
            }else if(sortType==1){
                list.sort(new Comparator<Integer>(){
                    @Override
                    public int compare(Integer int1,Integer int2){
                        return int2-int1;
                    }
                });
            }
            for(int i=0;i<list.size();i++){
                System.out.print(list.get(i));
                if(i!=list.size()-1){
                    System.out.print(" ");
                }
            }
           
        }
发表于 2023-04-13 22:39:01 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();

        int num[] = new int[a];
        for(int i = 0; i < a; i++) {
            num[i] = in.nextInt();
        }
       
        int b  = in.nextInt();

        Arrays.sort(num);

        if (b == 0) {
            for(int i = 0; i < a; i++) {
                System.out.print(num[i] + " ");
            }
        }else {
            for(int i = a-1; i >=0; i--) {
                System.out.print(num[i] + " ");
            }
        }
    }
}
发表于 2023-03-28 18:15:03 回复(0)
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            int[] array = new int[n];
            for (int i = 0; i < n; i++) {
                array[i] = sc.nextInt();
            }
            int flag = sc.nextInt();
            Arrays.sort(array);
            if (flag == 0) {
                for (Integer s : array) {
                    System.out.print(s + " ");
                }
            }else{
                for(int i=array.length-1;i>=0;i--){
                    System.out.print(array[i]+" ");
                }  
            }
        }
    }
}
发表于 2023-03-06 15:56:35 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int totalNum = in.nextInt();
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < totalNum; i++)
            arrayList.add(in.nextInt());
        int sort = in.nextInt();
        Collections.sort(arrayList);
        if (sort == 0) {
            for (Integer integer : arrayList)
                System.out.print(integer + " ");
        } else
            for (int i = arrayList.size() - 1; i >= 0; i--)
                System.out.print(arrayList.get(i) + " ");
    }
}

发表于 2022-12-29 20:11:50 回复(0)
合并排序:
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int num = in.nextInt();
        int i=0;
        int [] arr = new int[num];
        while (i<num){
            arr[i] = in.nextInt();
            i++;
        }
        int flag = in.nextInt();
        mergeSort(arr,0,num-1,flag);
        for (int i1 : arr) {
            System.out.print(i1+" ");
        }
    }

    public static void mergeSort(int[] arr,int first,int end,int flag){
        if(first>=end){
            return;
        }
        int mid = first+(end-first)/2;
        mergeSort(arr,first,mid,flag);
        mergeSort(arr,mid+1,end,flag);

        int[] tmp = new int[end-first+1];
        int i =0, length=end-first+1, j=first, k=mid+1;
        if(flag==0){
            //升序
            while (i<length){

                if(j>=first && j<=mid){
                    if(k>mid && k<=end){
                        if(arr[j]<arr[k]){
                            tmp[i]=arr[j];
                            j++;
                        }else {
                            tmp[i]=arr[k];
                            k++;
                        }
                    }else {
                        tmp[i]=arr[j];
                        j++;
                    }
                }else {
                    tmp[i]=arr[k];
                    k++;
                }

                i++;
            }
        }else{
            //降序
            while (i<length){

                if((j>=first && j<=mid)){
                    if((k>mid && k<=end)){
                        if(arr[j]>arr[k]){
                            tmp[i]=arr[j];
                            j++;
                        }else {
                            tmp[i]=arr[k];
                            k++;
                        }
                    }else {
                        tmp[i]=arr[j];
                        j++;
                    }
                }else {
                    tmp[i]=arr[k];
                    k++;
                }

                i++;
            }
        }

        for(i=0;i<length;i++){
            arr[first+i] = tmp[i];
        }
    }
}


发表于 2022-11-08 15:07:12 回复(0)
使用冒泡排序
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        int len,flag;
        int i,j;
        int tmp,f;
        while(scan.hasNext()){
            len = scan.nextInt();
            int[] num = new int[len];
            for(int k=0;k<len;k++){
                num[k] = scan.nextInt();
            }
            flag = scan.nextInt();
            if(flag == 0){
                for(j=len-1; j>0; j--){
                    for(i=1;i<=j;i++){
                        if(num[i-1] > num[i]){
                            tmp = num[i-1];
                            num[i-1] = num[i];
                            num[i] = tmp;
                        }
                    }
                }
            }else{
                for(j=len-1; j>0; j--){
                    for(i=1;i<=j;i++){
                        if(num[i-1] < num[i]){
                            tmp = num[i-1];
                            num[i-1] = num[i];
                            num[i] = tmp;
                        }
                    }
                }
            }
            for(i=0;i<len-1;i++){
                System.out.print(num[i] + " ");
            }
            System.out.println(num[len-1]);
        }
    }
}
发表于 2022-09-11 20:41:22 回复(1)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Integer[] numbers = new Integer[n];
        for(int i=0; i<n; i++){
            numbers[i] = in.nextInt();
        }
        int flag = in.nextInt();
        
        if(flag==0) Arrays.sort(numbers);
        else Arrays.sort(numbers, (a, b) -> b-a);
    
        for(int i : numbers){
            System.out.print(i + " ");
        }
    }
}

发表于 2022-08-05 20:10:55 回复(0)

问题信息

难度:
82条回答 38628浏览

热门推荐

通过挑战的用户

查看代码