I♥TLE

Java、オンラインジャッジなど

0001-List of Top 3 Hills

問題(外部リンク)

List of Top 3 Hills | Aizu Online Judge

実装の概要

今回の場合は入力が整数かつ10個と決まっているため、ソートした上で配列の後ろから見れば高い順に表示できます。降順ソートのためにComparatorを使うこともできますが今回はそこまでの必要はないと考えました。
なお、Arrays.sort()を使わない実装例も載せておきます。若干強引ですが、こちらでは「最大の要素を探して印をつけておく」→「印の付いているもの以外で最大の要素を探す」→「以下略…」という流れで解いています。

実装例1

public class Main {

    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int[] input = new int[10];

        for(int i = 0; i < 10; i++){
            input[i] = Integer.parseInt(br.readLine());
        }

        //小さい順にソート
        Arrays.sort(input);

        for(int i = 9 ; i >= 7; i--){
            System.out.println(input[i]);
        }

    }

}

実装例2

public class Main {
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         
        int[] input = new int[10];
        boolean[] checked = new boolean[10];
         
        for(int i = 0; i < 10; i++){
            input[i] = Integer.parseInt(br.readLine());
            checked[i] = false;
        }
         
        for(int i = 0 ; i < 3; i++){
            int maxIndex = -1;
            int max = -1;
             
            for(int j = 0; j < 10; j++){
                if(input[j] > max && checked[j] == false){
                    max = input[j];
                    maxIndex = j;
                }
            }
             
            checked[maxIndex] = true;
            System.out.println(input[maxIndex]);
        }   
    }
}