poj 1064 Cable master【浮点型二分查找】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29554 | Accepted: 6247 |
Description
To buy network cables, the Judging Committee has contacted a local network solutions provider with a request to sell for them a specified number of cables with equal lengths. The Judging Committee wants the cables to be as long as possible to sit contestants as far from each other as possible.
The Cable Master of the company was assigned to the task. He knows the length of each cable in the stock up to a centimeter,and he can cut them with a centimeter precision being told the length of the pieces he must cut. However, this time, the length is not known and the Cable Master is completely puzzled.
You are to help the Cable Master, by writing a program that will determine the maximal possible length of a cable piece that can be cut from the cables in the stock, to get the specified number of pieces.
Input
Output
If it is not possible to cut the requested number of pieces each one being at least one centimeter long, then the output file must contain the single number "0.00" (without quotes).
Sample Input
4 11
8.02
7.43
4.57
5.39
Sample Output
2.00
1、最后的r输出是要用%f
2、刚开始一直错,看了别人的题解,发现原来并不一定要把所有的电缆都用上如数据
2 2
1.02
2.33
答案为1.16
3、还有就是要注意最后的结果并不是四舍五入,而是直接取小数点后两位
#include<stdio.h>
#include<string.h>
#define MAX 10100
#define maxn(x,y)(x>y?x:y)
double a[MAX];
int n,m;
int num(double x)//计算在x长度时最多可以有多少段这样做
{ //就解决了不一定全部电缆都要用的问题
int i,sum=0;
for(i=1;i<=n;i++)
{
sum+=(int)(a[i]/x);
}
return sum;
}
int main()
{
int i;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
double max=0,suma=0;
for(i=1;i<=n;i++)
{
scanf("%lf",&a[i]);
max=maxn(max,a[i]);
suma+=a[i];
}
if(suma/m<0.01)
printf("0.00\n");
else
{
double l=0.01,r=max,mid=0;
while(r - l >1e-8)
{
mid=(l+r)/2.0;
if(num(mid)>=m)
l=mid;
else
r=mid;
}
int tem=r*100;
double ans=tem*0.01;
printf("%.2f\n",ans);
}
}
return 0;
}
poj 1064 Cable master【浮点型二分查找】的更多相关文章
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- [ACM] poj 1064 Cable master (二分查找)
Cable master Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21071 Accepted: 4542 Des ...
- POJ 1064 Cable master (二分查找)
题目链接 Description Inhabitants of the Wonderland have decided to hold a regional programming contest. ...
- POJ 1064 Cable master (二分)
题目链接: 传送门 Cable master Time Limit: 1000MS Memory Limit: 65536K 题目描述 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长 ...
- POJ 1064 Cable master 浮点数二分
http://poj.org/problem?id=1064 题目大意: 有N条绳子,他们的长度分别为Li,如果从它们中切割出k条长度相同的绳子的话,这K条绳子每条能有多长? 思路: 二分,设答案为m ...
- POJ 1064 Cable master(二分答案)
嗯... 题目链接:http://poj.org/problem?id=1064 其实这是一道很好想的二分答案的一道题... 二分的区间就是1~max_l,从1开始是因为所有小于1的都需要按0计算,没 ...
- poj 1064 Cable master ,二分 精度!!!
给出n根绳子,求把它们分割成K条等长的绳子的最大长度是多少? 二分 用 for(int i=0; i<100; ++i) 取代 while(r-l>eps) 循环100次精度能达到1e ...
- POJ 1064 Cable master (二分答案,G++不过,C++就过了)
题目: 这题有点坑,G++过不了,C++能过. 条件:n个数据a[],分成k段,结果精度要求两位小数. 问题:每段最长为多少? 思路:因为精度要求为两位小数,我先把所有的长度a[]*100. 我们对答 ...
- POJ 1064 Cable master 【二分答案】
和杭电那一题一样,只不过G++交不能通过,C++能过 wa了好多好多好多次----------------------------------------- #include<iostream& ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
随机推荐
- C++ sizeof总结
关键字sizeof的作用是返回对象或类型占用的内存字节数,返回值是size_t. 基本数据类型占用的字节数:(32位系统) char 1字节 bool 1字节 short 2字节 int 4字节 lo ...
- C# json与对象之间的相互转换
1. 添加命名空间 using System.Runtime.Serialization.Json; 2. WriteObject方法 // 从一个对象信息生成Json串 public static ...
- ios专题 - GCD(2)
何为Dispatch Sources 简单来说,dispatch source是一个监视某些类型事件的对象.当这些事件发生时,它自动将一个block放入一个dispatch queue的执行例程中. ...
- uniq和sort的用法
uniq和sort都是按行操作的linux命令. sort按文本行排序,如下所示的log文件:直接sort log即可将其排序. 容易忽略的是sort -n命令,在如下例子中将看到 如果直接sort则 ...
- 345. Reverse Vowels of a String(C++)
345. Reverse Vowels of a String Write a function that takes a string as input and reverse only the v ...
- jquery roundabout幻灯片插件3d图片切换支持图片旋转切换滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 轻松使用px为单位开发移动端页面
研究移动端页面已经有许久了,一直执着于rem来开发,不谈性能怎么样,单从工作效率上看影响了不少,首先要固定设计稿的宽度,一般都是固定在640px,然后在根据根目录的字体大小来计算出每个元素的rem的值 ...
- PHPCMS标签:get标签
GET标签源自于PHPCMS 2008版,其使用SQL语句直接获取数据的特性,成为大家制作模板的首选. 在V9中这样强大的工具也得到保留下来. GET标签使用方式如下: {pc:get sql=&qu ...
- C# 数据结构 基础 论述
问题: 信息世界中,计算机是加工处理的信息的载体,在这个过程中面临着三个问题: 1.如何方便高效的组织数据 2.如何在计算机中存储数据(内存和外存) 3.如何对存储的数据进行高效的操作 目的: 我们都 ...
- 关于UPdate用法的
updaterestore_base set restore_base.localcost =(select localcost from [nt2000\cpi].chongia2. ...