import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class 绝对值排序 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); while (sc.hasNext()) { int n=sc.nextInt(); if (n==0) { break; } Integer A[]=new Integer[n]; //输入 for (int i = 0; i < A.length; i++) { A[i]=sc.nextInt(); } Arrays.sort(A,new Comparator<Integer>() { public int compare(Integer a,Integer b){ return Math.abs(b)-Math.abs(a); }; }); for (Integer i : A) { System.out.print(i+" "); } System.out.println(); } } }
解题思路:
自定义排序鬼咋,实现Comparator方法
注意事项:
参考代码:
0.0分
1 人评分
C二级辅导-求偶数和 (C语言代码)浏览:629 |
C语言程序设计教程(第三版)课后习题4.9 (C语言代码)浏览:692 |
2003年秋浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:695 |
这可能是一个假的冒泡法浏览:986 |
打水问题 (C语言代码)浏览:1072 |
C语言程序设计教程(第三版)课后习题7.3 (C语言代码)浏览:1195 |
C语言训练-数字母 (C语言代码)浏览:649 |
C语言程序设计教程(第三版)课后习题8.4 (C语言代码)浏览:619 |
DNA (C语言代码)浏览:540 |
矩阵加法 (C语言代码)浏览:1722 |