博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Can you solve this equation? 详细解答
阅读量:6655 次
发布时间:2019-06-25

本文共 2969 字,大约阅读时间需要 9 分钟。

你的支持是我最大的动力,你的意见是我前进的导航。

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
2100-4
 
Sample Output
1.6152No solution!
 
Author
Redow
 
 
Recommend
lcy

题目大意就是x∈[0, 100], Y∈[-10^10, 10^10],求8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 = Y 的解。

题目很简单,首先先需要些前期工作,通过一导,二导就会发现,其实对于 f(x) = 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6, x∈[0, 100]时,这个函数是递增的。所以用二分法即可解题。

代码如下

1 #include 
2 #include
3 double f (double x) //for convenience 4 { 5 return 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6; 6 } 7 int main() 8 { 9 int T;10 double x1, x2, x3, y, y1, y2, y3;11 scanf("%d", &T);12 while(T--)13 {14 x1 = 0;15 x2 = 100; 16 scanf("%lf", &y); //heed17 y1 = f(x1) - y;18 y2 = f(x2) - y;19 if (y1 > 0 || y2 < 0)20 printf("No solution!\n");21 else22 {23 while (fabs(y1 - y2) >= 0.0001)24 {25 x3 = (x1 + x2) / 2;26 y3 = f(x3) - y;27 if (y3 >= 0)28 x2 = x3;29 else30 x1 = x3;31 y1 = f(x1) - y;32 y2 = f(x2) - y;33 }34 printf("%0.4f\n", x3);35 }36 }37 return 0;38 }

 

 

 

1 #include 
2 #include
3 double f (double x) //for convenience 4 { 5 return 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6; 6 } 7 int main() 8 { 9 int T;10 double x1, x2, x3, y, y1, y2, y3;11 scanf("%d", &T);12 while(T--)13 {14 x1 = 0;15 x2 = 100; 16 scanf("%lf", &y); //heed17 y1 = f(x1) - y;18 y2 = f(x2) - y;19 if (y1 > 0 || y2 < 0)20 printf("No solution!\n");21 else22 {23 while (fabs(x1 - x2) >= 0.000001)24 {25 x3 = (x1 + x2) / 2;26 y3 = f(x3) - y;27 if (y3 >= 0)28 x2 = x3;29 else30 x1 = x3;31 }32 printf("%0.4f\n", x3);33 }34 }35 return 0;36 }

 

注意点:

1、应该花大部分时间在思路上,编码应该快速完成

2、尽量用double,用double时,要注意用"%lf"输入。float 有38位,double有308位

3、这个题目要求x精确到4位,刚开始时,我以为我以x取到4位小数为结束条件,一直错!后来来发现应该以y1,y2很接近为结束结束条件。原因很简单以为x取到4位时,有可能这个y还很不精确。当然,也可以让x1和x2很接近,如上面的second program也是对的。

4、C中有fabspow,在math.h头文件中。

转载于:https://www.cnblogs.com/chuanlong/archive/2013/01/16/2862731.html

你可能感兴趣的文章
Python之简单理解装饰器(1)
查看>>
阿里云发布 Redis 5.0 缓存服务:全新 Stream 数据类型带来不一样缓存体验
查看>>
java中switch使用的数据类型
查看>>
mysql innodb plugins
查看>>
linux修复丢失的分区表
查看>>
【python】操作oracle数据库
查看>>
Symantec BE2012 0xe000fec9 报错
查看>>
iOS 开发遇到的问题
查看>>
RMAN duplicate 复制数据库 window平台
查看>>
单臂路由的实现
查看>>
delphi枚举wmi
查看>>
国内安全管理平台应用发展对比分析
查看>>
埃森哲:2017年网络犯罪成本研究报告(含分析)
查看>>
tomcat启动startup.bat一闪而过
查看>>
STL源码剖析之算法:power
查看>>
DELL服务器硬盘指示灯的显示说明
查看>>
做一个Cubieduino如何(有新内容了)?
查看>>
我的友情链接
查看>>
mysql学习一 DDL(数据定义语言)
查看>>
java栈的两种实现方法
查看>>