二分查找

二分查找又称折半查找,优点是比较次数少,查找速度快,平均性能好;其缺点是要求待查表为有序表,且插入删除困难。因此,折半查找方法适用于不经常变动而查找频繁的有序列表。首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
 
 
这道题就是高中的一个定理,好像是零点定理。
零点定理:
设函数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. 网站banner写法
  2. PowerDesigner 逆向工程 Mariadb 失败
  3. 【转载】使用pandas进行数据清洗
  4. assert的用法
  5. 将excel数据导入到mysql的方法
  6. Linux磁盘管理之元数据、文件和目录、链接文件03
  7. ajax是异步的,异步取数据,如何能保证数据是存在的。
  8. 尽量采用as操作符而不是旧式C风格做强制类型转换
  9. C++ 局部变量的析构
  10. 自己封装的SqlHelper
  11. 一段网上java常见escape和unescape方法的BUG
  12. poj 2271HTML
  13. webpack3中使用postcss-loader和autoprefixer给css3样式添加浏览器兼容
  14. MongoDB with D3.js
  15. 我的2017&amp;2018
  16. Day12 Python基础之生成器、迭代器(高级函数)(十)
  17. Android 开发 框架系列 OkHttp使用详解
  18. Docker 简单查看name和ip
  19. ubuntu 4.10~5.10 :古老的ubuntu上安装oracle10g的情况
  20. 《A_Pancers团队》———团队项目原型设计与开发

热门文章

  1. Delphi函数详解:全局函数,内部函数,类的成员函数,类的静态方法
  2. Java IO 之 File 的创建、重命名与遍历
  3. DPDK 分析
  4. BZOJ 1023: [SHOI2008]cactus仙人掌图 | 在仙人掌上跑DP
  5. [poj 2274]后缀数组+最长公共子串
  6. Codeforces Round #538 (Div. 2) (A-E题解)
  7. Could not resolve com.android.support:multidex:1.0.2
  8. angular-translate加载.json文件进行翻译
  9. fscanf函数的应用
  10. 【bzoj3926- [Zjoi2015]诸神眷顾的幻想乡】广义sam