zoukankan      html  css  js  c++  java
  • Can you solve this equation?(二分查找的简单应用)

    Can you solve this equation?

    Time Limit:1000MS    Memory Limit:32768KB    64bit IO Format:%I64d & %I64u

    SubmitStatus

    Description

    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.
     

    Input

    The 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);
     

    Output

    For 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!
     思路:
    记f(x)为等式左边的值,如果f(100.0)小于Y或f(0.0)大于Y,无解,否则必有解,且解的范围为[0.0, 100.0],不断二分区间,直至找到f(x) = Y

     1 #include <iostream>
     2 #include <string>
     3 #include <cstdio>
     4 #include <cmath>
     5 #include <cstring>
     6 #include <algorithm>
     7 #include <map>
     8 #include <vector>
     9 #include <queue>
    10 #include <stack>
    11 #define LL long long
    12 #define MAXI 2147483647
    13 #define MAXL 9223372036854775807
    14 #define eps (1e-8)
    15 #define dg(i) cout << "*" << i << endl;
    16 
    17 using namespace std;
    18 
    19 double Solve(double x)
    20 {
    21     return (8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6);
    22 }
    23 
    24 int main()
    25 {
    26     int t;
    27     double y, low, high, mid;
    28     scanf("%d", &t);
    29     while(t--)
    30     {
    31         scanf("%lf",&y);
    32         low = 0.0;
    33         high = 100.0;
    34         if(y > Solve(100.0) || y < Solve(0.0)) puts("No solution!");
    35         else
    36         {
    37             while(low + eps < high)
    38             {
    39                 mid = (low + high) * 0.5;
    40                 if(y > Solve(mid) + eps) low = mid;
    41                 else if(y < Solve(mid) - eps) high = mid;
    42                 else break;
    43             }
    44             printf("%.4lf\n", mid);
    45         }
    46     }
    47     return 0;
    48 }


  • 相关阅读:
    回调那些事儿
    v-if和v-show小对比
    导出下载功能
    vue和react
    Redis 实现抢票
    MySQL 各种连接,
    MySQL的分组,降序 实现
    MySQL 窄表转宽表
    EX: 判断密码, 判断字符必须包含大写,小写,数字,特殊字符 ,并且键盘不能连续
    hive 基础
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910404.html
Copyright © 2011-2022 走看看