码迷,mamicode.com
首页 > 其他好文 > 详细

【POJ】1064Cable master

时间:2018-05-12 21:41:30      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:inf   nbsp   小数   floor   bool   分析   can   scanf   style   

白皮书的二分

 

题意:有n条绳子,他们的长度分别为li,从他们中切个出k条长度相同的绳子的话,这k条绳子每条最长能有多长?保留小数点后两位

分析:二分

#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn 10010
#define INF 100001
double a[maxn];
int n,k;

bool dix(double x)
{
    int num=0,i;
    for(i=0;i<n;++i)
        num+=(int)(a[i]/x);
    return num>=k;//满足条件是返回true,不满足返回false 
}

int main()
{
    while(scanf("%d%d",&n,&k)!=EOF)
    {
        int i;
        for(i=0;i<n;++i)
            scanf("%lf",&a[i]);
        double left=0,right=INF,mid;
        i=1000;
        while(i--)
        {
            mid=(left+right)/2;
            if(dix(mid))
                left=mid;
            else
                right=mid;
        }
        printf("%0.2f\n",floor(right*100)/100);//注意精度,可不明白%0.2lf输出为什么会错 
    }
    return 0;
} 

 

【POJ】1064Cable master

标签:inf   nbsp   小数   floor   bool   分析   can   scanf   style   

原文地址:https://www.cnblogs.com/Kohinur/p/9029921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!