版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/40347105

二分搜索 :

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <sstream>
#include <stdlib.h>
#include <malloc.h>

using namespace std;

int n;
int k;
double p[20000];

int c (double x)
{
    int num = 0;
    for (int i=1;i<=n;i++)
    {
        num+= (int)(p[i]/x);
    }
    return (num >= k);
}

int main ()
{

    while (scanf ("%d%d",&n,&k)!=EOF)
    {
        for (int i=1;i<=n;i++)
            scanf ("%lf",&p[i]);

        double lf=0,rl=10000000;
        double mid;
        for (int i=0;i<100;i++)
        {
            mid = (lf + rl)/2;
            if (c(mid))
            {
                lf=mid;
            }
            else
            {
                rl=mid;
            }
        }
        printf("%0.2lf\n",floor(lf * 100)/100);
    }
    return 0;
}