现在的位置: 首页 > 综合 > 正文

UVa10341 Solve It!

2018年12月21日 ⁄ 综合 ⁄ 共 590字 ⁄ 字号 评论关闭

题意:

解方程:  

输入格式:

   输入包含不超过2100组数据。每行为一组数据,包含6个整数p, q, r, s, t, u (0<=p,r<=20, -20<=q, s, t <=0) 。

输出格式:

        对于每组数据,输出所有解,按照从小到大顺序排列,每个解均保留小数点后4位。如果无解,输出 No  solution 。


分析:

在0<=x<=1时, 方程f(x)=0前五项都是减函数, 而最后一项是常数。 所以当f(0)>=0 且f(1)<=0时f(x)=0有唯一解,否则无解。

 

code:

#include <stdio.h>
#include <math.h>
#define F(x) (p*exp(-x) + q*sin(x) + r*cos(x) +s*tan(x) + t*(x)*(x) +u)

const double eps=  1e-14;

int main()
{
    int p, r,q, s, t, u;
    while(scanf("%d%d%d%d%d%d",&p, &q, &r, &s, &t, &u) == 6)
    {
        double f0 = F(0), f1 = F(1);
        if(f1 >eps || f0<-eps) printf("No solution\n");
        else
        {
            double x = 0, y =1, m;
            for(int i=0; i<100; ++i)
            {
                m = x + (y-x)/2;
                if(F(m) <0) y = m;
                else x = m;
            }
            printf("%.4lf\n", m);
        }
    }
    return 0;
}

抱歉!评论已关闭.