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

本文共 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

你可能感兴趣的文章
WordPress友情链接插件:Auto BlogRoll
查看>>
搭建或者升级Python环境笔记,吐血记录!
查看>>
如何在haproxy的后端服务器查看客户端的IP?
查看>>
网络编程:端口,InetAddress,Socket【简】
查看>>
权限模型体系设计
查看>>
JVM垃圾回收与性能调优总结
查看>>
Linux使用shell自动切换网关
查看>>
Spring 的优秀工具类盘点,第 1 部分: 文件资源操作和 Web 相关工具类
查看>>
谨防非法网络传销网站finnciti 前身smi已被取缔
查看>>
Kubernetes使用Ceph静态卷部署应用
查看>>
为CentOS 6.5 配置本地YUM源
查看>>
linux grep命令
查看>>
Memcache知识点梳理
查看>>
1.java用户校验
查看>>
【MySQL】lower_case_table_names参数详解
查看>>
定时任务crond生产实战经验
查看>>
mysql-5.5配置主从 及 主主关系
查看>>
高级文件系统管理
查看>>
磁盘阵列RAID的功能作用介绍
查看>>
安装discuz
查看>>