poj 1064 (二分+控制精度) && hdu 1551
链接:http://poj.org/problem?id=1064
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23896 | Accepted: 5118 |
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 ×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
要注意控制精度,以及最后的舍入误差。
脑残犯了个基础的错误,
// sum+=(int)str[i]/x;
这样强制转型,就有问题了,应该把要转型的部分用括号括起来,两边都要
舍入误差在于,如果结果是1.0099999
应该输出1.00,而不是1.01
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h> #define eps 1e-6 double str[];
int n,k; int judge(double x)
{
int sum=;
for(int i=; i<n; i++)
{
sum+=(int)(str[i]/x);
}
return sum;
} int main()
{
int i,j;
while(scanf("%d%d",&n,&k)!=EOF)
{
double left=0.0,right,sum=0.0,maxx=0.0;
//ght=n/k;
for(i=; i<n; i++)
{
scanf("%lf",&str[i]);
//if(maxx < str[i])
// maxx = str[i];
//sum+=str[i];
maxx=maxx<str[i]?str[i]:maxx;
}
right=maxx;
while(right-left > eps)
{
double mid=(left+right)/2.0;
if(judge(mid) >= k )
left=mid;
else
right=mid;
}
int tmp=right * ;
double ans=(double)tmp * 0.01;
printf("%.2lf\n",ans);
}
return ;
}
poj 1064 (二分+控制精度) && hdu 1551的更多相关文章
- POJ 1064 (二分)
题目链接: http://poj.org/problem?id=1064 题目大意:一堆棍子可以截取,问要求最后给出K根等长棍子,求每根棍子的最大长度.保留2位小数.如果小于0.01,则输出0.00 ...
- poj 1064(二分答案)
传送门:Problem 1064 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有N条绳子,长度分别为 length[1,2,3,.. ...
- POJ 1064 Cable master(二分查找+精度)(神坑题)
POJ 1064 Cable master 一开始把 int C(double x) 里面写成了 int C(int x) ,莫名奇妙竟然过了样例,交了以后直接就wa. 后来发现又把二分查找的判断条 ...
- poj 1064 Cable master 判断一个解是否可行 浮点数二分
poj 1064 Cable master 判断一个解是否可行 浮点数二分 题目链接: http://poj.org/problem?id=1064 思路: 二分答案,floor函数防止四舍五入 代码 ...
- POJ 1064 1759 3484 3061 (二分搜索)
POJ 1064 题意 有N条绳子,它们长度分别为Li.如果从它们中切割出K条长度相同的绳子的话,这K条绳子每条最长能有多长?答案保留小数点后2位. 思路 二分搜索.这里要注意精度问题,代码中有详细说 ...
- POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划)
POJ 2342 Anniversary party / HDU 1520 Anniversary party / URAL 1039 Anniversary party(树型动态规划) Descri ...
- POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流)
POJ 2711 Leapin' Lizards / HDU 2732 Leapin' Lizards / BZOJ 1066 [SCOI2007]蜥蜴(网络流,最大流) Description Yo ...
- POJ - 2018 二分+单调子段和
依然是学习分析方法的一道题 求一个长度为n的序列中的一个平均值最大且长度不小于L的子段,输出最大平均值 最值问题可二分,从而转变为判定性问题:是否存在长度大于等于L且平均值大于等于mid的字段和 每个 ...
- 二分搜索 POJ 1064 Cable master
题目传送门 /* 题意:n条绳子问切割k条长度相等的最长长度 二分搜索:搜索长度,判断能否有k条长度相等的绳子 */ #include <cstdio> #include <algo ...
随机推荐
- xml、 Dao service 三层参数以及对应关系
=======service 调用dao用params.put(K,Value);将参数传入后台. BaseResponse response = new BaseResponse(); Map& ...
- 161021、spring异步调用,完美解决!
前言 项目中,用户抢单,下单需要向对方推送消息,但是加上推送就会造成抢单和下单性能降低,反应变慢,因为抢单下单动作跟推送部分是同步的,现在想改成异步推送. 在Java应用中,绝大多数情况下都是通过同步 ...
- 161013、java实现邮件群发带附件
要完成Java群发邮件功能,首先须加入mail.jar和activation.jar这两个包 下面是邮件的例子: import java.io.File; import java.util.Prope ...
- scala特质
package com.ming.test /** * scala 特质,类似与java接口,但是比java接口强大,可以有实现方法,定义字段之类的 */ /** * 定义一个日志的特质 */ tra ...
- 搬瓦工的ShadowSock设置方法:
- json-encode()怎么进行解码呢?
解决中文的一种方法就是先将中文转换为另一种编码格式,然后再使用json_encode(),最后再用解码把json串进行解码.还有一种方式就在php新版本中得到了解决,在下面的代码为展示. 以下为代码示 ...
- Volley Get Post 方法
Get String url = CommonInterfaceUrl.COMM_GetWorksDetailUrl + "/" + worksID; RequestQueue m ...
- PHP获取客户端真实IP的自定义函数
<?php //虽然使用 $_SERVER[REMOTE_ADDR] 也可以获取,但是要获取真实的客户端IP地址,需要使用下面的方法: function _getIP(){ $ip=getenv ...
- [thml]HTML select标签 获取选中的option的value及Text内容
很简单的select标签: <select id="hello" onchange="getContent(this.value,this.options[this ...
- HDU 4712:Hamming Distance
Hamming Distance Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...