2012/02/29

[zerojudge] a104 排序

題目:https://zerojudge.tw/ShowProblem?problemid=a104
說明:Java 中利用 Arrays 來協助,好方便。

Java 版
import java.util.Arrays;
import java.util.Scanner;

public class A104 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int size = Integer.parseInt(in.nextLine());
            String [] str = in.nextLine().split(" ");
            int [] numb = new int [size];
            for(int i = 0; i < size; i++) {
                numb[i] = Integer.parseInt(str[i]);
            }
            Arrays.sort(numb);
            for (int j : numb) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
Python 版 (2022.07)
while True:
    try:
        a = int(input())
        lst = list(sorted(map(int, input().split(' '))))
        for i in range(a):
            print(lst[i], end=' ')
        print()
    except EOFError:
        break

沒有留言:

張貼留言