-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary search
54 lines (52 loc) · 1.08 KB
/
binary search
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<cstdio>
//二分查找 算法笔记 例子
const int n=10;
int binaryserach(int a[],int left,int right,int x){
int mid;
while(left <= right){
mid=(left+right)/2;
if(a[mid] == x) return mid;
else if( a[mid]< x) left =mid+1;
else right =mid-1;
}
return -1;
}
int main(){
int a[n]={1,3,4,6,7,8,10,11,12,15};
int m;
while( scanf("%d",&m) !=EOF){
int x=binaryserach(a ,0,n-1,m);
printf("%d\n",x);
}
return 0;
}
算法笔记 装水问题
#include<cstdio>
#include<cmath>
const double PI =acos(-1.0);
const double eps = 1e-5;//精度
double f( double R,double h){
double alpha = 2* acos( (R-h) /R);
double L = 2*sqrt(R *R -(R-h) *(R-h));
double S1 = alpha *R *R/2 - L*(R- h)/2;
double S2 = PI *R *R/2;
return S1/S2;
}
double solve( double R,double r){
double left =0,right =R,mid;
while( right - left >eps){
mid = (left + right) /2;
if( f(R,mid) > r){
right = mid;
}else{
left =mid;
}
}
return mid;
}
int main(){
double R,r;
scanf("%lf%lf",&R,&r);
printf("%.4f\n", solve(R ,r ));
return 0;
}