ECNU 2844 排序去重

ECNU 2844 排序去重

链接

https://acm.ecnu.edu.cn/problem/2844

题目

单点时限: 2.0 sec

内存限制: 256 MB

有 个 到 之间的整数 ,对于其中重复的数字,只保留一个,把其余相同的数去掉。然后再按照指定的排序方式把这些数排序。

输入格式
第 1 行为字母 A 或 D,A 表示按照升序排序,D 表示按照降序排序。

第 2 行开始有若干个用一个空格或换行符分隔的正整数。

输出格式
相互之间用一个空格分隔的经去重和排序后的正整数。最后一个数后没有空格。

样例
input
A
20 40 32 67 40 20 89 300 400 15
output
15 20 32 40 67 89 300 400

思路

多加了一个判断的排序,拿个string存一下ad就行,之后控制一下输出。

代码

  public static void fun() {
    Scanner sc = new Scanner(System.in);
    String mod = sc.next();

    ArrayList<Integer> list = new ArrayList<>();
    while (sc.hasNextInt()) {
      int temp = sc.nextInt();
      list.add(temp);
    }
    if (mod.charAt(0)=='A') {
      Collections.sort(list);
    } else {
      list.sort(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
          return o2-o1;
        }
      });
    }

    Iterator<Integer> it = list.iterator();
    int tag = 1001;
    int flag = 1;
    while (it.hasNext()) {
      int temp = it.next();
      if (temp != tag) {
        if (flag != 1) {
          System.out.print(" ");
        }
        System.out.print(temp);
        flag++;
      }
      tag = temp;
    }
  }

原文地址:https://www.cnblogs.com/blogxjc/p/14325928.html