题解 | #输入整型数组和排序标识,对其元素进行排序#

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

https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
import java.util.stream.Collectors;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Integer[] arr = new Integer[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        int asc = sc.nextInt();
        Comparator<Integer> comparator = (a, b) -> a - b;
        if (asc == 1) {
            comparator = (a, b) -> b - a;
        }
        Arrays.sort(arr, comparator);
        String s = Arrays.stream(arr).map(String::valueOf).collect(Collectors.joining(" "));
        System.out.println(s);
    }
}

全部评论

相关推荐

10-10 00:14
门头沟学院 Java
程序员小白条:20年架构师,无工资
点赞 评论 收藏
分享
拿到了ssp的小鲸鱼...:满分160做90+是吧
投递中国电信等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务