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 ...
随机推荐
- Linux 封闭端口和安全
两种方法: 1.停掉没用的服务,关闭端口监听 2.用 iptables 封闭端口 ######################################## 1.查看端口和占用服务 netsta ...
- C++注意事项锦集
1.std::string的.length()方法 计算代‘/’的字符串的长度会少计算‘/’的个数.比如:[warning]*****[/warning]
- postgres创建用户,表
使用createuser来创建用户 [postgres@web1 ~]$ /data/pgsql/bin/createuser --help createuser creates a new Post ...
- 使用python脚本监控weblogic
1.python的脚本如下: ############################################################################### #crea ...
- nohup DEMO
nohup,顾名思义:挂起免疫. nohup命令可以防止当你退出系统时,在后台运行的进程被终止.它能让你运行的命令或脚本在你退出系统后继续在后台运行. nohup命令不能自动的将任务放在后台运行,所以 ...
- greenplum集群安装
一.环境配置 1.地址分配 192.168.1.201 mdw master 192.168.1.202 sdw1 segment1 192.168.1.203 sdw2 segment2 2.创建用 ...
- 标准类型String(学习中)
1.读取string对象 #include<iostream> #include<cstring> using namespace std; int main() { stri ...
- rtc关机闹钟4 AlarmManagerService.java
vim base/services/core/java/com/android/server/AlarmManagerService.java AlarmManager 调用 IAlarmManage ...
- SlickGrid example 8:折线图
根据数据生成折线图,使用相当简单,也很容易. 主要方法: 数据: var vals = [12,32,5,67,5,43,76,32,5]; 生成折线图: $("testid&quo ...
- java 四舍五入 保留俩位小数
public static void main(String[] args) { String str="0"; BigDeci ...