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
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!”;
世上无难事,只怕有心人!

浙公网安备 33010602011771号