hdu 2199
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 <stdio.h>
#include <string>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
double cmp(double low,double high,double y);
double mcp(double x);
int main()
{
int T;
double n,m;
while(cin>>T){
while(T--){
cin>>n;
if(n<mcp(0)||n>mcp(100))
cout<<"No solution!"<<endl;
else {
m=cmp(0,100,n);
cout<<setiosflags(ios::fixed)<<setprecision(4)<<m<<endl;}
}
}
return 0;
}
double cmp(double low,double high,double y)
{
double mid;
while(high-low>1e-10){
mid=(low+high)/2.0;
if(mcp(mid)>y) high=mid;
else low=mid;
}
return mid;
}
double mcp(double x){
return 8.0*x*x*x*x+7.0*x*x*x+2.0*x*x+3.0*x+6.0;
}
递归法代码:
如果递归的判断终止条件为low<high,时间超限;因为这个类型是double型,有些数会递归很多次以至n次才会的到结果,以至超时;由于提上要求是4位小数,所以只需要让low<high-0.0000001即可;
#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
double cmp(double low,double high,double y);
double mcp(double x);
int main()
{
int T;
double n,m;
while(cin>>T){
while(T--){
cin>>n;
if(n<mcp(0)||n>mcp(100))
cout<<"No solution!"<<endl;
else {
m=cmp(0,100,n);
cout<<setiosflags(ios::fixed)<<setprecision(4)<<m<<endl;}
}
}
return 0;
}
double cmp(double low,double high,double y)
{
double mid=(low+high)/2;
if(high-low>1e-10){
if(mcp(mid)>y)
return cmp(low,mid,y);
else if(mcp(mid)<y)
return cmp(mid,high,y);
else return mid;
}
return mid;
}
double mcp(double x){
return 8.0*x*x*x*x+7.0*x*x*x+2.0*x*x+3.0*x+6.0;
}
思路:先要判断这个函数的单调性,之后在就容易多了,给你一个数y;求解出对应的x,如果x是在0-100之间输出x浮点数为4;否则输出“No solution!”;
说白了就是把y移到等式左边是f(x)= 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 -Y;求是零点的值;如果x是在0-100之间输出x,浮点数为4;否则输出“No solution!”;
hdu 2199的更多相关文章
- 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 == ...
- hdu 2199 (二分)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 ...
- hdu 2199 Can you solve this equation?(高精度二分)
http://acm.hdu.edu.cn/howproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/1000 MS ...
- (二分搜索)Can you solve this equation? -- hdu -- 2199
链接: http://acm.hdu.edu.cn/showproblem.php?pid=2199 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDU 2199 Can you solve this equation?(二分解方程)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2199 Can you solve this equation? Time Limit: 2000/10 ...
- ACM:HDU 2199 Can you solve this equation? 解题报告 -二分、三分
Can you solve this equation? Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Su ...
- hdu 2199 Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu 2199 Can you solve this equation?
#include<stdio.h> #include<math.h> double f(double x) { return 8*x*x*x*x+7*x*x*x+2*x*x+3 ...
- hdu 2199:Can you solve this equation?(二分搜索)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 2199 Can you solve this equation? (二分 水题)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
随机推荐
- JS中,如何查询一个对象的所有属性
var res = ""; for(var p in object) { res += p + ","; } alert(res);
- 轻量级jQuery工具提示插件tooltipsy使用方法
今天想给单位的系统弄一个提示插件,懒得自己做,于是就上网百度了几个jQuery插件,挺好用的.下面以tooltipsy插件为例,说明如何使用这些插件. 一.下载 首先到tooltipsy的官网http ...
- 直播 linux上的第一个c++程序
这里用crt来直播吧: 登录到服务器上之后: 1.mkdir -p 建一个文件夹 2.ll 查看一下这个文件夹 3.cd 进入这个文件夹 4.vim 进行文本编辑 写完之后,按下esc中止输入模式,用 ...
- 俄罗斯人开发的等待控件TfgActivityDialog
http://blog.csdn.net/star1010/article/details/28674173
- 从vector容器中查找一个子串:search()算法
如果要从vector容器中查找是否存在一个子串序列,就像从一个字符串中查找子串那样,次数find()与find_if()算法就不起作用了,需要采用search()算法:例子: #include &qu ...
- 关于group by
<pre name="code" class="sql">关于group by 排序问题 10g 以前sort group by 需要排序 10g ...
- Best Cow Line (POJ 3617)
题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构 ...
- 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪
能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而是可以按片自由搭配 | 36氪 能取悦生理期的女性吗?Le Parcel提供女性卫生用品按月订购服务,不是按包出售而 ...
- <转载>Win32控制台工程中创建窗口
有的时候,用控制台同步输出调试信息.程序状态量,比出Log.弹出报错对话框等方法来得有效.那么如何做到呢?如下: 简而言之,用GetModuleHandle()函数获得当前程序实例句柄,其它地方与常见 ...
- Java 微服务框架 Redkale 入门介绍
Redkale 功能 Redkale虽然只有1.xM大小,但是麻雀虽小五脏俱全.既可作为服务器使用,也可当工具包使用.作为独立的工具包提供以下功能:1.convert包提供JSON的序列化和反序列化功 ...