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

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

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

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);
    int count = in.nextInt();
    // 技巧
    List<Integer> list = new ArrayList<>(count);
    while (count > 0) {
      count--;
      list.add(in.nextInt());
    }

    boolean asc = in.nextInt() == 0;
    if (asc) {
      Collections.sort(list);
    } else {
      Collections.sort(list, Collections.reverseOrder());
    }

    System.out.println(list.stream()
                       .map(String::valueOf)
                       .collect(Collectors.joining(" ")));

  }
}

全部评论

相关推荐

东孝子_强东我偶像:你怎么当孝子都和我时间一样😭
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务