【POJ - 1064】Cable master(二分)

Cable master

直接上中文了

Descriptions

输入2个数 N  K

n条绳子    要分成大于等于k段

求每段最长多长呢?并且每段不能小于1cm

必须以厘米精度写入数字,小数点后正好是两位数。
如果无法切割所请求的每个长度至少为1厘米的件数,则输出文件必须包含单个数字“0.00”(不带引号)。

多组文件输入

Sample Input

4 11
8.02
7.43
4.57
5.39

Sample Output

2.00

题目链接

https://vjudge.net/problem/POJ-1064

 

这个输出是个坑点,不能直接用double,然后 printf("%.2f\n")这会自动四舍五入,是错误的,我看其他大佬的方法是,输入时先*100输出的时候/100就能解决这个输出问题

其他的没什么,就是二分,不懂的再看看代码吧

 

AC代码

复制代码
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define Mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 10000+5
using namespace std;
int l,r;//左,右
int N,K;
int main()
{
    while(cin>>N>>K)
    {
        int len[Maxn];//绳子
        for(int i=0; i<N; i++)
        {
            double t;
            cin>>t;
            len[i]=t*100.0;
        }
        sort(len,len+N);//排序,直接找最大值
        l=1,r=len[N-1];
        int ans=0;//答案
        while(l<=r)
        {
            int mid=(l+r)/2;
            int cnt=0;//计数器,是否大于等于K
            for(int i=0; i<N; i++)
                cnt+=len[i]/mid;
            if(cnt>=K)
            {
                l=mid+1;
                ans=max(ans,mid);//找最大值
            }
            else
                r=mid-1;
        }
        printf("%.2f\n",(double)ans/100.0);
    }
    return 0;
}
复制代码

 

posted on   Sky丨Star  阅读(1090)  评论(1编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示