2016年1月13日 星期三

a104: 排序

內容 :
幫我排個數字謝謝QQ
輸入說明 : 
有多筆測資以EOF為結束
第一行有一個正整數n(1<=n<=1000),代表有幾個數字要請你幫忙排
第二行有n個可以用int儲存的正整數
輸出說明 : 
輸出n個已由小到大排序好的正整數
範例輸入 : help
6
7 9 0 4 1 8
8
1 9 9 0 0 9 2 8
範例輸出:
0 1 4 7 8 9
0 0 1 2 8 9 9 9
提示 : 
標籤:

import java.util.*;
public class a104 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int temp = 0;
int[] m=new int[n];
for(int i=0;i<n;i++)
m[i]=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(m[i]<m[j]){
temp=m[i];
m[i]=m[j];
m[j]=temp;
}
}
}
for(int i=0;i<n;i++){
  System.out.print(m[i]+" ");
}
System.out.println();
}
}

}

沒有留言:

張貼留言