zoukankan      html  css  js  c++  java
  • UVA 10341 solve it

    Solve the equation:
    p∗e−x + q∗sin(x) + r∗cos(x) + s∗tan(x) + t∗x2 + u = 0
    where 0 ≤ x ≤ 1.
    Input
    Input consists of multiple test cases and terminated by an EOF. Each test case consists of 6 integers in a single line: p, q, r, s, t and u (where 0 ≤ p, r ≤ 20 and −20 ≤ q,s,t ≤ 0). There will be maximum 2100 lines in the input file.
    Output
    For each set of input, there should be a line containing the value of x, correct up to 4 decimal places, or the string ‘No solution’, whichever is applicable.
    Sample Input
    0 0 0 0 -2 1 1 0 0 0 -1 2 1 -1 1 -1 -1 1
    Sample Output
    0.7071 No solution 0.7554

    观察题目中的条件,方程必定在定义域内单调递减,逻辑方面就很容易了。

    在实现上,使用二分找到一个满足条件的解,不过我一开始使用的whlie循环居然会超时,后来换成for循环就过了,后来才发现我的循环有问题

    while(abs(f(mid))>=MIN)
                {
                    if(f(mid)>=MIN)
                    fir=mid,mid=(fir+las)/2;
                    else
                    las=mid,mid=(fir+las)/2;
            }

    判断结束的精确度不应该是用函数值,而应该是用mid,下面for循环通过代码

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<stack>
    #include<map>
    using namespace std;
    const int MAX=1e6;
    const double MIN=1e-6;
    #define INF 0x7fffffff
    #define ll long long
    #define FOR(i,n) for(i=1;i<=n;i++)
    #define mst(a) memset(a,0,sizeof(a))
    #define mstn(a,n) memset(a,n,sizeof(a))
    //struct num{int a,b,i;}a[1005];
    //bool cmp(const num &x, const num &y){return x.a>y.a;}
    int  p,q,r,s,t,u;
    float f(float x)
    {
        return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;
    }
    
    int main()
    {
        while(scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)!=EOF)
        {
            double fir=0,las=1,mid=(fir+las)/2;
            if(f(fir)<0||f(las)>0) 
            cout<<"No solution"<<endl;
            else
            {
                for(int i=0;i<=100;i++)
                {
                    mid=(las+fir)/2;
                    if(f(mid)>MIN)
                    fir=mid;
                    else
                    las=mid;
                }
                printf("%.4lf
    ",mid);
          }
      }
    return 0; }
  • 相关阅读:
    Matplotlib介绍
    过拟合与欠拟合
    EM最大期望算法
    深度学习之卷积神经网络CNN及tensorflow代码实例
    朴素贝叶斯及贝叶斯网络简介
    迁移学习简介
    Tensorflow 之物体检测
    tensorflow object detection API
    ubuntu17.04中启动Tensorboard过程
    deep learning 经典网络模型之Alexnet、VGG、Googlenet、Resnet
  • 原文地址:https://www.cnblogs.com/qq936584671/p/7126179.html
Copyright © 2011-2022 走看看