欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

NUSOJ 2671 二分法应用之解方程

程序员文章站 2024-03-16 21:29:16
...

二分法应用之解方程8x^4 + 7x^3 + 2x^2 + 3x + 6
NUSOJ 2671

Problem:

Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 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!

题意:先输入t,代表有t个测试数据,然后输入测试的数也就是Y,然后根据8 * x^4 + 7 * x^3 + 2 * x^2 +3*x+ 6 = Y,求出x的值,很简单的二分题目。

#include<bits/stdc++.h>
using namespace std;

double vis(double y){
    return 8*pow(y,4)+7*pow(y,3)+2*pow(y,2)+3*y+6;
}

int main()
{
    int t;
    cin>>t; //t个测试数据
    while(t--){
          double y; 
           cin>>y;//x在1~100之间,y不会超过807020306,不会小于6
         if(y>807020306 || y<6) printf("langxinxuechang!\n");
         else {
            double left=0,right=100,mid;
            while(right-left>0.000000001){ 
             mid = left + (right-left)/2 ;

               if(vis(mid)==y) break;
               else if(vis(mid)>y) right = mid;
               else if(vis(mid)<y) left = mid;

            }
            printf("%.4f\n",mid);
         }




 }
    return 0;
}