1007
来源:互联网 发布:linux 软件安装方式 编辑:程序博客网 时间:2024/05/21 09:22
题目编号:1007
题目大意:给N节绳子,需要分成K段,给定N节绳子每节的长度,求能分得K段最长是多少。
解题思路:这个不是很难,典型的二分查找。首先建立数组存储绳子长度,然后二分开始查找,建立一个判断函数,如果在当前mid条件下,能够达到K条则输出,不能达到继续二 分循环查找下去。
解题感想:一直Getting complication error information failed!我也不知道怎么回事,改了差不多7-8次,最后才发现是输出的问题,被我弄混了。。。也不知道当时怎么想的。A了 就好 心情顿时开心了,不然睡不着啊。现在终于可以睡觉了。。。
#include<iostream>#include<iomanip>using namespace std;double a[10001];int N,K;int check(double x){ int i,sum=0; for(i=0;i<N;i++) sum+=(int)(a[i]/x); if(sum<K) return 0; return 1;}int main(){ int i; double left,right,mid,max; while(cin>>N>>K) { if(N==0&&K==0) break; max=0; for(i=0;i<N;i++) { cin>>a[i]; if(a[i]>max) max=a[i]; } left=0; right=max; while(right-left>0.001) { mid=(right+left)/2; if(check(mid)) left=mid; else right=mid; } cout<<fixed<<setprecision(2)<<mid<<endl; } return 0;}
0 0
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- 1007
- Xcode 【本地git 篇(2)】
- ProgressBar进度条相关
- Android FrameWork——Touch事件派发过程详解
- linux
- 严重: WSSERVLET11: failed to parse runtime descriptor: The serviceName cannot be retrieved from an int
- 1007
- ARM Linux 3.x的设备树(Device Tree)
- HDU 2063过山车
- java web笔记——随机图片验证码实现看不清换一张
- vim常用快捷键
- light oj1265 Island of Survivalm
- WebStorm下使用TypeScript
- Device Tree(一):背景介绍
- poj 2709 Painter