觉得浮夸了四年,漠然发现原来是浮躁了四年!

二分搜索(binary search ) problem(hdu 2199)

 problem link address :http://acm.hdu.edu.cn/showproblem.php?pid=2199

在顺序搜索的数组实现中,如果把分治思想思想应用于搜索过程,就会大大缩短搜索的总时间。

实现方法:把数据分为两部分,确定搜索关键字属于那一部分然后集中搜索这一部分,然后依次类推,每次把处理的部分分为两部分,因此,这种方法称为“二分法”。

关于本题:典型的二分查找。应为本题有明显的特征: 查找范围0~100,并且有唯一解。

Code
 1 #include<iostream>
 2 #include<cmath>
 3 using namespace std;
 4 double ans(double a)
 5 {
 6     double s=8*a*a*a*a+7*a*a*a+2*a*a+3*a+6;
 7     return s;
 8 }
 9 int main()
10 {
11     int T;
12     cin>>T;
13     while(T--)
14     {
15         double num;
16         cin>>num;
17         if(num<ans(0)||num>ans(100))
18         {
19             cout<<"No solution!"<<endl;
20             continue;
21         }
22         double left=0;
23         double right=100;
24         while(left<right)
25         {
26             double mid=(right+left)/2.0;
27             double temp=ans(mid);
28             if(fabs(temp-num)<=1e-6)
29             {
30                 printf("%.4lf\n",mid);
31                 break;
32             }
33             else if(temp>num)
34                 right=mid;
35             else 
36                 left=mid;
37         }
38     
39     }    return 0;
40 }
41 
42 
43 
44 
45 
46         

 

posted @ 2012-11-22 17:56  heat nan  阅读(141)  评论(0)    收藏  举报