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 ( ...
随机推荐
- mysql 5.6 参数详解
系统变量提供的是各种与服务器配置和功能有关的信息.大部分的系统变量都可以在服务器启动时进行设置.在运行时,每一个系统变量都拥有一个全局值或会话值,或者同时拥有这两个值.许多系统变量都是动态的,也就是说 ...
- spark原理介绍
1.spark是一个基于内存计算的开源的集群计算系统,目的是让数据分析更加快速.因此运行spark的机器应该尽量的大内存,如96G以上. 2.spark所有操作均基于RDD,操作主要分成2大类:tra ...
- n阶行列式计算----c语言实现(完结)
花了半天时间,写了这个n阶行列式计算的程序,应该算是比较优美吧,有很多地方多次做了优化,程序占用内存不是很大,要是说小吧,也不合适,因为里边有一个递归,而且递归的深度还比较深.时间复杂度具体没有细看, ...
- 使用Android平板编程,执行linux命令
android有一些应用支持开发, AIDE 介绍http://www.wandoujia.com/apps/com.aide.ui https://play.google.com/store/app ...
- pyzmq简单的在线聊天室
#encoding=utf-8#客户端 import zmq c = zmq.Context() s = c.socket(zmq.REQ) s.connect('tcp://127.0.0.1:10 ...
- Eclipse项目导入Android Stuio 配置出现 Timeout waiting to lock buildscript class cache for build file 'H:\studioproject\Generic_SN\build.gradle'
Eclipse项目导入Android Stuio 配置出现 Error:Timeout waiting to lock buildscript class cache for build file ...
- 使用GCD的dispatch_once创建单例
使用GCD的dispatch_once创建单例 介绍了创建单例的方式,不过后来发现其实在ios 4.0后有更加简单的方式. 就是使用GCD的功能 代码如下: + (instantClass *)sha ...
- IOS 使用新浪微博SDK
项目需要,测试了一下新浪微博SDK,现在记录一下初步使用过程. 1.下载导入新浪SDK. 2.去新浪开发者平台申请APPkey等. 3.工程中使用 4.在 APPDelegate中写两行 [Weibo ...
- cf B. Levko and Permutation
http://codeforces.com/contest/361/problem/B #include <cstdio> #include <cstring> #includ ...
- 用Nginx实现Session共享的均衡负载
前言 大学三年多,也做个几个网站和APP后端,老是被人问到,如果用户多了服务器会不会挂,总是很尴尬的回答:“哈哈,我们的用户还少,到了服务器撑不住的时候,估计都上市了吧”.说是这么说,但是对于有强迫症 ...