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
Source
#include <iostream> #include <cstdio> #include <cmath> using namespace std; int n,k; double l,r; double s[10001]; int sum(double t) { int c = 0; for(int i = 0;i < n;i ++) c += (int)(s[i] / t); return c; } int main() { scanf("%d%d",&n,&k); for(int i = 0;i < n;i ++) { scanf("%lf",&s[i]); r = max(r,s[i]); } int i = 0; while(i ++ <= 100) { double mid = (l + r) / 2; int su = sum(mid); if(su < k)r = mid; else l = mid; } printf("%.2f",(int)(l * 100) / 100.0); }
还有一种方法是转换成整数。