很水的二分找答案,不过,需要注意精度的控制
#include <stdio.h>
const double eps= 1e-8;
int data[10010], m, n, ans;
bool judge(int mod)
{
int cnt= 0;
for ( int i = 0 ; i < n ; i++ )
cnt+= data[i]/mod;
return cnt >= m;
}
int main()
{
while ( 2 == scanf("%d%d", &n, &m ) )
{
int i, sum= 0;
bool flag= true;
double t;
for ( i = 0 ; i < n ; i++ )
{
scanf("%lf", &t);
data[i]= (int)((t+eps)*1000);
sum+=data[i];
(flag && sum < m*10) ? flag= true : flag= false;
}
if ( flag )
{
printf("0.00\n");
continue;
}
int left= 0, right= 1000000000, mid;
while ( left < right )
{
mid= left+right>>1;
if ( judge(mid) )
left= mid+1;
else ans = right = mid-1;
}
int a= ans/1000, b= (ans-a*1000)/10, c= (ans-a*1000)%10;
printf("%d.", a);
if ( c > 9 )
b++;
if ( b < 10 )
printf("0%d\n", b);
else printf("%d\n", b);
}
return 0;
}