二分查找

二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难。因此,折半查找方法适用于不经常变动而查找频繁的有序列表。首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
 
 
这道题就是高中的一个定理,好像是零点定理。
零点定理:
设函数f(x)在闭区间[a,b]上连续,且f(a)与 f(b)异号(即f(a)× f(b)<0),那么在开区间(a,b)内至少有函数f(x)的一个零点,即至少有一点ξ(a<ξ<b)使f(ξ)=0。-----百度百科
头尾相乘为<0,则有解,>0则无解。
 

UVA10341.Solve It

 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

代码如下:

#include<stdio.h>
#include<math.h>
const double eps=1e-7;
int p,q,r,s,t,u;
double fun(double 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)){
double maxx=1.0,minn=0.0,mid;
if(fun(maxx)*fun(minn)>){
printf("No solution\n");
continue;
}
while(minn+eps<maxx){
mid=(maxx+minn)/2.0;
if(fun(mid)<=) maxx=mid;
else minn=mid;
}
printf("%.4f\n",mid);
}
return ;
}
 
 

最新文章

  1. QQ空间动态爬虫
  2. X3D中Profile如何翻译
  3. Hibernate入门案例配置以及增、删、改、查看
  4. bzoj 1208 splay模板题2
  5. TP复习3
  6. python: 生成guid
  7. Yum出错Error: Cannot find a valid baseurl for repo: base
  8. VS2003.NET在文件中查找卡死
  9. 【译】ASP.NET MVC 5 教程 - 4:添加模型
  10. hibernate---性能优化, 1+N问题
  11. XMLHttpRequest中常用的方法
  12. [Swift]LeetCode551. 学生出勤纪录 I | Student Attendance Record I
  13. CentOS7.5 下搭建SFTP
  14. 撸一个小型PHP框架
  15. 【原】gulp工作中的实战
  16. python接口自动化测试(一)-环境准备
  17. es5中for...in 和es6中 for..of遍历
  18. cisco 3850 GBIC报错处理
  19. SQLSERVER2008中创建数据库发生无法获得数据库&#39;model&#39;上的排他锁
  20. December 24th 2016 Week 52nd Saturday

热门文章

  1. CSS图片文字同行居中
  2. Hive实际应用小结
  3. php多语言切换---转载
  4. python+selenium自动化测试_1
  5. vue过滤数字为2位小数点,过滤器
  6. 环形进度条的实现方法总结和动态时钟绘制(CSS3、SVG、Canvas)
  7. getComputedStyle与currentStyle获取样式(style/class)
  8. javascript计算对象的长度
  9. JS中typeof和instanceof用法区别
  10. Go同步和异步执行多个任务封装