zoukankan      html  css  js  c++  java
  • HDU 2199 Can you solve this equation?(二分精度)

    HDU 2199 Can you solve this equation?
     
     
    Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100; 
    Now please try your lucky.

    InputThe first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);OutputFor each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.Sample Input

    2
    100
    -4

    Sample Output

    1.6152
    No solution!

    这种题一般都很注重精度的,可以拿两个端点来做验证,6的时候应该为0.0000,807020306的时候应该为100.0000
    其中while(ri-le<=0.000000001)这里的0尽量多一点,而判断的时候的0的个数要看情况,有些当满足
    ri-le<=0.000000001时,说不定判断还不满足。

    #include <iostream>
    #include <stack>
    #include <string.h>
    #include <stdio.h>
    #include<queue>
    #include<algorithm>
    #define ll long long
    using namespace std;
    int main()
    {
        int t;
        cin>>t;
        while(t--)
        {
            double y;
            cin>>y;
            double le,ri,mid;
            le=0;ri=100;
            bool f=0;
            while(ri-le>=0.00000000000001)
            {
                mid=(le+ri)/2;
                //cout<<8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y<<endl;
                if(8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y<0.00001&&8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y>=0)
                {
                    f=1;
                    break;
                }
                else if(8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6-y<0)
                {
                    le=mid;
                }
                else
                    ri=mid;
            }
            if(f)
                printf("%.4lf
    ",mid);
            else
                printf("No solution!
    ");
        }
        return 0;
    }
     
  • 相关阅读:
    MSDN仿站
    跟我学android—02.CustomActivity
    iptables redirect outside requests to 127.0.0.1
    linux 查看端口使用情况
    防火墙、Iptables、netfilter/iptables、NAT 概述
    POSTROUTING与PREROUTING区别
    android:layout_gravity和android:gravity的区别
    EasyUI datagrid 分页Json字符串格式
    [转载]easyui datagrid 时间格化(JS 日期时间本地化显示)
    [转载]EasyUI Pagination 分页的两种做法
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270919.html
Copyright © 2011-2022 走看看