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 人评分
简单的a+b (C语言代码)浏览:524 |
A+B for Input-Output Practice (IV) (C语言代码)浏览:451 |
C二级辅导-阶乘数列 (C语言代码)浏览:688 |
1642题解浏览:708 |
出圈】指针malloc版浏览:355 |
C二级辅导-等差数列 (C语言代码)浏览:694 |
企业奖金发放 (C语言代码)浏览:2313 |
排序算法(选择,插入,冒泡)浏览:774 |
简单的a+b (C语言代码)浏览:504 |
简单的a+b (C语言代码)浏览:580 |