201612-1 中間數

時間複雜度是n方,外循環遍歷數組,內循環統計數目

奉上java滿分代碼

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        String[] line = scanner.nextLine().split(" ");
        int[] numbers = new int[n];
        for(int i = 0; i < n; i++){
            numbers[i] = Integer.parseInt(line[i]);
        }
        scanner.close();

        int number = -1;
        for(int numI : numbers){
            int bigger = 0;
            int smaller = 0;
            for(int numJ : numbers){
                if(numI > numJ)
                    bigger++;
                if(numI < numJ)
                    smaller++;
            }
            if(smaller == bigger){
                number = numI;
                break;
            }
        }
        System.out.println(number);
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章