UVa 10341

another simple DC problem be solved by bisection method
notice to the 3 conditions when current x is smaller than desired x
/* UVas */
/*****************************************************************************/
int p, q, r, s, t, u;
double calc(double mid) {
    return p*exp(-mid)+q*sin(mid)+r*cos(mid)+s*tan(mid)+t*mid*mid+u;
}
int main(void) {
    while (cin>>p>>q>>r>>s>>t>>u) {
        double l=0.0, h=1.0, mid, x, y;
        while (h-l>0.000000001) {
            mid=(l+h)/2;
            x=calc(mid);
            y=calc(h);
            if((y<0&x>0)||(y>=0&&x>y)||(y<=0&&y>x)) {
                l=mid;
            } else {
                h=mid;
            }
        } 
        abs(x)<=0.0001?printf("%.4f\n", mid):printf("No solution\n");
    }
    return 0;
}
/*****************************************************************************/

No comments :

Post a Comment