ECNU 2975 排序

ECNU 2975 排序

链接

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

题目

单点时限: 2.0 sec

内存限制: 256 MB

有 个 到 之间的整数,对于其中重复的数,只保留一个,把其余相同的数去掉。然后再按照个位数字进行升序排序,如果个位数字相同,则小的数排在前面。

输入格式
第 行:整数 () 为问题数。

第 ~ 行:每一个问题两行,第一行整数个数 ,第二行 个用一个空格分隔的正整数。

输出格式
对于每个问题,输出一行问题的编号( 开始编号,格式:case #0: 等),然后在一行中输出经去重和排序后的正整数,两个数之间用一个空格分隔。最后一个数后没有空格。行末尾输出一个换行符。

样例
input
2
10
20 40 32 67 40 20 89 300 400 15
18
2 1 2 1 2 1 2 1 20 20 40 32 67 40 89 300 400 15
output
case #0:
20 40 300 400 32 15 67 89
case #1:
20 40 300 400 1 2 32 15 67 89

思路

遇事不决比较器,重写一下顺序就行了,优先比较个位数,取余可以得到,之后比较整体数字大小。

代码

public static void fun() {
    Scanner sc = new Scanner(System.in);
    int t = sc.nextInt();
    for (int i = 0; i < t; i++) {
      ArrayList<Integer> list = new ArrayList<>();
      int n = sc.nextInt();
      for (int j = 0; j < n; j++) {
        int temp = sc.nextInt();
        list.add(temp);
      }
      list.sort(new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
          int a = o1 % 10, b = o2 % 10;
          if (a != b) {
            return a - b;
          } else {
            return o1 - o2;
          }
        }
      });
      Iterator<Integer> it = list.iterator();
      int tag = 1001;
      int flag = 1;
      System.out.println("case #" + i + ":");
      while (it.hasNext()) {
        int temp = it.next();
        if (temp != tag) {
          if (flag != 1) {
            System.out.print(" ");
          }
          System.out.print(temp);
          flag++;
        }
        tag = temp;
      }
      System.out.println();
    }


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