YT14-HDU-在0-100间找到那个正确的x

Problem 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!

代码如下:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double Y;
double Equ(double x)
{
    return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;             //判断是否等式是否相等
}

double Search()                                               //二分查找
{
    double high=100,low=0;
    double temp;
    while (high-low>1e-6)
    {
        temp=(low+high)/2;
        if (Equ(temp)<Y)
            low=temp+1e-7;
        else
            high=temp-1e-7;
    }
    return (high+low)/2.0;
}

int main()
{
    int T;
    cin>>T;
    while (T--)
    {
        cin>>Y;
        if (Equ(0)<=Y&&Equ(100)>=Y)                    //如果满足条件,说明0到100中间肯定存在一个值使得Equ(x)=Y
            cout<<setiosflags(ios::fixed)<<setprecision(4)<<Search()<<endl;
        else
            cout<<"No solution!"<<endl;
    }
    return 0;
}

解题思路:

题目大意是在0到100间找到满足 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y 的那个x。

简单的二分查找,重点是计算精度和输出格式。



时间: 2024-08-10 19:17:49

YT14-HDU-在0-100间找到那个正确的x的相关文章

C语言 &#183; 打印1~100间的质数(素数)

算法提高 c++_ch02_04 时间限制:1.0s   内存限制:256.0MB 问题描述 输出1~100间的质数并显示出来.注意1不是质数. 输出格式 每行输出一个质数. 2 3 ... 97 注意:素数又称质数. 1 #include<stdio.h> 2 #include<math.h> 3 int main(){ 4 int m,i,k,h=0,leap=1; 5 for(m=2;m<=100;m++){ 6 k=sqrt(m+1); 7 for(i=2;i<

解决“出现Unable to retrieve 192.168.0.100/var/www/html/images/install.img错误”

分析:我们必须了解这个错误出现在哪个阶段才能正确找到错误原因,出现这个错误时已经是在开机PXE引导顺利从dhcp server中获得了ip,(或者 光盘引导)均正常,而且正常的读取了ks文件,正确的找到了安装源,否则不会提示无法获取install.img文件.一般无法获取某个文件,不是该文件损坏就是网络不通的问题. 解决办法: 1)我们先证明安装源服务器是正常的,我们用其他机器下载install.img文件,使用下面的命令: wget http://192.168.0.100/var/www/h

hdu 4370 0 or 1 (最短路)

hdu 4370 0 or 1 Description Given a n*n matrix C ij (1<=i,j<=n),We want to find a n*n matrix X ij (1<=i,j<=n),which is 0 or 1. Besides,X ij meets the following conditions: 1.X 12+X 13+-X 1n=1 2.X 1n+X 2n+-X n-1n=1 3.for each i (1 < i < n

Vue2.0组件间数据传递

Vue1.0组件间传递 使用$on()监听事件: 使用$emit()在它上面触发事件: 使用$dispatch()派发事件,事件沿着父链冒泡: 使用$broadcast()广播事件,事件向下传导给所有的后代 Vue2.0后$dispatch(),$broadcast()被弃用,见https://cn.vuejs.org/v2/guide/migration.html#dispatch-和-broadcast-替换 1,父组件向子组件传递场景:Father上一个输入框,根据输入传递到Child组件

52.从键盘上输入若干学生成绩(成绩在0~100之间),计算平均成绩,并输出低于平均分的学生成绩,用输入负数结束输入

//1.建立一个for循环用于输入数据,设置退出条件 //2.算出平均成绩 #include<iostream> using namespace std; int main() { int Score,sum=0,k=0; int a[100]; float Average; cout<<"please input some students's score:"<<endl; for(int i=0;i<100;i++) { cin>&g

【JavaScript】随机生成10个0~100的数字

随机生成10个0~100不重复的数字(包含0和100): 需要用到的知识点:随机数 去重 下面放代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> // 定义声明一个数组, 放随机生成的10个数字 var arr = []; for(var

通过属性选择器找元素,可以通过$(__).length是否为0来判断是否找到了元素

通过属性选择器找元素,可以通过$("").length是否为0来判断是否找到了元素. 为0的时候表示没有找到,其余则返回找到了多少个. 不能通过$("")是否为null来判断 原文地址:https://www.cnblogs.com/zhoushiya/p/12107320.html

尚未在 Web 服务器上注册 ASP.NET 2.0。为了使网站正确运行,可能需要手动将 Web 服务器配置为使用 ASP.NET 2.0,按 F1 可了解更多详细信息。

装了win7操作系统后,vs2010运行项目后出现“尚未在 Web 服务器上注册 ASP.NET 2.0.为了使网站正确运行,可能需要手动将 Web 服务器配置为使用 ASP.NET 2.0,按 F1 可了解更多详细信息.”的提示,点击确定后项目可以编译成功,但是在浏览器上运行时却提示“HTTP 错误 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效.” 错误代码 0x80070021 这个错误是因为IIS 7 采用了更安全的Web.C

HDU 4370 0 or 1(spfa+思维建图+计算最小环)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4370 题目大意:有一个n*n的矩阵Cij(1<=i,j<=n),要找到矩阵Xij(i<=1,j<=n)满足以下条件: 1.X 12+X 13+...X 1n=1  2.X 1n+X 2n+...X n-1n=1  3.for each i (1<i<n), satisfies ∑X ki (1<=k<=n)=∑X ij (1<=j<=n). 举个例子