d587: 參貳壹真好吃
題目:http://zerojudge.tw/ShowProblem?problemid=d587
程式碼:
package zerojudge;
import java.util.Scanner;
public class d587 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();//有幾個數字
int [] arr = new int[n];//陣列
for(int i=0; i<arr.length; i++){//用輸入方式存入陣列
arr[i] = sc.nextInt();
}
for(int i=0; i<arr.length; i++){ //排序
for(int j=0;j<arr.length-1;j++)
if(arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
for(int arr1 : arr){
System.out.print(arr1+" ");
}
//System.out.print(arr[i]);
}
}
}
import java.util.Arrays;
import java.util.Scanner;
public class d587 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();//有幾個數字
int [] arr = new int[n];//陣列
for(int i=0; i<arr.length; i++){//用輸入方式存入陣列
arr[i] = sc.nextInt();
}
Arrays.sort(arr);//引入排序方法
for(int arr1 : arr){
System.out.print(arr1+" ");
}
//System.out.print(arr[i]);
}
}
}

留言
張貼留言