博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 10341 solve it
阅读量:4562 次
发布时间:2019-06-08

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

Solve the equation:

p∗e−x + q∗sin(x) + r∗cos(x) + s∗tan(x) + t∗x2 + u = 0
where 0 ≤ x ≤ 1.
Input
Input consists of multiple test cases and terminated by an EOF. Each test case consists of 6 integers in a single line: p, q, r, s, t and u (where 0 ≤ p, r ≤ 20 and −20 ≤ q,s,t ≤ 0). There will be maximum 2100 lines in the input file.
Output
For each set of input, there should be a line containing the value of x, correct up to 4 decimal places, or the string ‘No solution’, whichever is applicable.
Sample Input
0 0 0 0 -2 1 1 0 0 0 -1 2 1 -1 1 -1 -1 1
Sample Output
0.7071 No solution 0.7554

观察题目中的条件,方程必定在定义域内单调递减,逻辑方面就很容易了。

在实现上,使用二分找到一个满足条件的解,不过我一开始使用的whlie循环居然会超时,后来换成for循环就过了,后来才发现我的循环有问题

while(abs(f(mid))>=MIN)            {                if(f(mid)>=MIN)                fir=mid,mid=(fir+las)/2;                else                las=mid,mid=(fir+las)/2;        }

判断结束的精确度不应该是用函数值,而应该是用mid,下面for循环通过代码

#include
#include
#include
#include
#include
#include
#include
using namespace std;const int MAX=1e6;const double MIN=1e-6;#define INF 0x7fffffff#define ll long long#define FOR(i,n) for(i=1;i<=n;i++)#define mst(a) memset(a,0,sizeof(a))#define mstn(a,n) memset(a,n,sizeof(a))//struct num{int a,b,i;}a[1005];//bool cmp(const num &x, const num &y){return x.a>y.a;}int p,q,r,s,t,u;float f(float x){ return p*exp(-x)+q*sin(x)+r*cos(x)+s*tan(x)+t*x*x+u;}int main(){ while(scanf("%d%d%d%d%d%d",&p,&q,&r,&s,&t,&u)!=EOF) { double fir=0,las=1,mid=(fir+las)/2; if(f(fir)<0||f(las)>0) cout<<"No solution"<
MIN) fir=mid; else las=mid; } printf("%.4lf\n",mid);  }   } return 0;}

 

转载于:https://www.cnblogs.com/qq936584671/p/7126179.html

你可能感兴趣的文章
C# unsafe模式内存操作深入探索
查看>>
Redis拾遗(一)
查看>>
js字符串转换为Json对象的三种写法
查看>>
Is it possible to display icons in a PopupMenu?
查看>>
Atitit.常见的4gl 第四代编程语言 与 dsl
查看>>
Atitit js es5 es6新特性 attilax总结
查看>>
JavaWeb学习记录(三)——网页中文编码问题
查看>>
$( document ).ready()&$(window).load()
查看>>
关于Baidu Map(百度地图SDK)的各种骚b问题!
查看>>
喜欢的一些话(不断更新)
查看>>
mysql 自动记录数据插入及最后修改时间
查看>>
c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格...
查看>>
ZT 80-90年代港台300部电视剧 你看过多少?
查看>>
C/C++关于全局变量和局部变量初始化与不初始化的区别
查看>>
题目1007:奥运排序问题
查看>>
爬虫实例——爬取1元夺宝用户头像(借助谷歌浏览器开发者工具)
查看>>
双目立体匹配经典算法之Semi-Global Matching(SGM)概述:匹配代价计算之Census变换(Census Transform,CT)...
查看>>
制作导航条
查看>>
iOS中的内存管理1
查看>>
23种设计模式全解析
查看>>