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
这道题的基本题意为给你N段电缆,要求剪成K段等长的电缆,求可以剪成的最大长度。这道题为基本的二分问题,假设剪成的长度已知,然后求可以剪成的段数,然后跟要求减的段数进行比较,如果多的话,就二分增大长度,如果少的话,就二分减小长度,知道达到要求的精确值为止。
源代码如下:
#include<iostream>
#include<iomanip>
#include<stdio.h>
double a[10005];
int n,k;
using namespace std;
int dp(double g)
{ int i,m=0;
for(i=0;i<n;++i)
m+=int(a[i]/g);
return m;
}
int main()
{ int i;
double min,max,mid,num=0;
scanf("%d%d",&n,&k);
max=0;
num=0;
for(i=0;i<n;++i)
{
scanf("%lf",&a[i]);
num+=a[i];
}
min=0;
max=num;
while(max-min>0.0001)
{
mid=(min+max)/2;
if(dp(mid)>=k)
min=mid;
else max=mid;
}
printf("%.2f\n",int(max*100)/100.0);
}
需要注意的是输入输出要用sacnf跟printf,并且输出结果要精确到小数点后两位。
6294

被折叠的 条评论
为什么被折叠?



