Poj 1064 Cable master, binary precision !!!, Poj1064
Given n ropes, how long is it to cut them into K equal-length ropes?
Binary
Use for (int I = 0; I <100; ++ I) instead of while (r-l> eps)
The precision of 100 cycles can reach 1-30, which can basically meet the precision requirements of general questions.
The binary interval of a floating point number is prone to an endless loop due to missing precision.
# Include <cstdio> double L [10000 + 10]; int n, k; int OK (double x) {int cnt = 0; for (int I = 0; I <n; ++ I) {cnt + = L [I] * 1.0/x;} return cnt> = k;} int main () {while (~ Scanf ("% d", & n, & k) {double Max = 0; for (int I = 0; I <n; ++ I) {scanf ("% lf", & L [I]); if (Max <L [I]) Max = L [I];} double l = 0, r = Max; for (int I = 0; I <100; ++ I) {double mid = (l + r)/2; if (OK (mid )) l = mid; else r = mid;} printf ("%. 2f \ n ", int (l * 100)/100.0);} return 0 ;}