絕對值排序
Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 124226????Accepted Submission(s): 58373
Problem Description
輸入n(n<=100)個整數(shù)奕锌,按照絕對值從大到小排序后輸出沛申。題目保證對于每一個測試實例,所有的數(shù)的絕對值都不相等忘朝。
Input
輸入數(shù)據(jù)有多組冕广,每組占一行,每行的第一個數(shù)字為n,接著是n個整數(shù),n=0表示輸入數(shù)據(jù)的結(jié)束,不做處理肪康。
Output
對于每個測試實例,輸出排序后的結(jié)果幽告,兩個數(shù)之間用一個空格隔開梅鹦。每個測試實例占一行裆甩。
Sample Input
3 3 -4 2
4 0 1 2 -3
0
Sample Output
-4 3 2
-3 2 1 0
代碼:
import java.util.Arrays;
import java.util.Scanner;
public class Main{
? ? ? public static void main(String []args ){
? ? ? Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
? ? ? int n=sc.nextInt();
? ? ? if(n==0) System.exit(0);
? ? ? else{
? ? ? int [ ]a=new int[n];
? ? ? for(int i=0;i<n;i++){
? ? ? a[i]=sc.nextInt();
? ? ? }
? ? ? for(int i=0;i<n;i++){
? ? ? for(int j=n-1;j>i;j--){
? ? ? if((Math.abs(a[i]))<(Math.abs(a[j]))) {
? ? ? int temp;
? ? ? temp=a[i];
? ? ? a[i]=a[j];
? ? ? a[j]=temp;
? ? ? }
? ? ? }
? ? ? }
? ? ? for(int j=0;j<a.length;j++) {
? ? ? if(j!=a.length-1)
? ? ? System.out.print(a[j]+" ");
? ? ? else
? ? ? System.out.println(a[j]);
? ? ? }
}
}
}
}