P1577 切绳子

二分答案

#include<cstdio>
using namespace std;
double a[10005];
int b[10005], n, k;
bool judge(int x)
{
    int s = 0;
    for(int i = 0; i < n; i++)
        s += b[i] / x;
    return s >= k;
}
int main()
{
    int l = 0, r = 10000000, mid;
    scanf("%d %d", &n, &k);
    for(int i = 0; i < n; i++)
        scanf("%lf", &a[i]), b[i] = a[i] * 100;
    while(l <= r)
    {
        mid = (l + r) >> 1;
        if(!mid)//l=0, r=0会死循环 
            break;
        if(judge(mid))
            l = mid + 1;
        else
            r = mid - 1;
    }
    printf("%.2f", r / 100.0);
    return 0;
} 

 

上一篇:C#用链式方法表达循环嵌套


下一篇:爱上MVC系列~过滤器实现对响应流的处理