今日頭條3-16後端開發實習第四題

 public static boolean check(int n,int m,double l,int []a) {
        int count = 0;
        for (int i = n - 1; i >= 0 && a[i] >= l; --i) {
            count += Math.floor(a[i] / l);
        }
        return count >= m;
    }
    public static double solve(int n,int m,int []a) {
        double l=0,r=a[n-1];
        while(r-l>0.01) {
            double mid = (r + l) / 2;
            if(check(n,m,mid,a)){
               l = mid;
            }else{
                r = mid - 0.01;
            }
        }
        return l;
    }

    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int  []a =new int[n];
        for(int i = 0; i < n; ++i){
           a[i]=scanner.nextInt();
        }
        Arrays.sort(a);
        double d = solve(n,m,a);
        BigDecimal b = new BigDecimal(d);
        d = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(d);

    }

 

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