C - Cable master POJ - 1064

Solution: using the binary search, the output decimal problems, usually specified number of allowable error range or behind the decimal point in the output. Therefore, when using the binary search method, it is necessary to set a reasonable end condition to meet the accuracy requirements.

Is set as the termination condition of the number of cycles, a cycle can be reduced to half the interval range, is 100 cycles up to 10 ^ (- 30) range of accuracy, substantially no problem, the accuracy may be set to ( ub-lb)> eps Thus, a specified section size. In this case, if the eps made too small, it is possible because of floating point precision into an infinite loop.
 
Original link: https: //blog.csdn.net/zhouzi2018/article/details/82918701

Note: This output is really fans

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#include<iomanip>
#define endl '\n'
#define _for(i,a,b) for(int i=a;i<b;i++)
#define EPS 1e-10
using namespace std;
const int N = 1e4+5;
typedef long long ll;
double Y ;  
double a[N];
int n,m;
bool check(double x){//TOO LONG
    int cnt = 0;
    _for(i,1,n+1){
        cnt+= int( a[i]/x );
    }    
    return cnt>=m;
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    _for(i,1,n+1){
        cin>>a[i];
    } 
    double L = 0,R = 1e6+5,t =100;
    while( fabs(R-L) > EPS ){
        double mid = (L+R)/2;      
        if( check(mid) ) L = mid;
        else R = mid;
    }
     
    cout<<fixed<<setprecision(2)<< floor(L*100)/100 <<endl;
    return 0;
}

 Another approach

https://www.cnblogs.com/nyist-xsk/p/7264846.html

Guess you like

Origin www.cnblogs.com/SunChuangYu/p/12601992.html