2014
03-16

# Volume of a cylinder

One cylinder across another cylinder at right angles, tell the volume of the intersection of the cylinders.

Multiple test cases, the number of them T is given in the very first line.
For each test case, two integers r1 and r2, meaning the radius of the two cylinders. ( 1 < r1, r2 < 80 )

Multiple test cases, the number of them T is given in the very first line.
For each test case, two integers r1 and r2, meaning the radius of the two cylinders. ( 1 < r1, r2 < 80 )

1
4 4

341.33

#include <iostream>
#include <stdio.h>
#include <set>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdlib.h>
using namespace std;
const int N = 1000000;

double smallArea( double r1, double r2, double	x){
return (sqrt(r1*r1 - x*x)*sqrt(r2*r2 - x*x));
}

double getArea( double r1, double r2){
double area = 0;
double dx = r1/(N*1.0);
double x;
for(int i=0; i<N; i++){
x = i*dx*1.0+0.5*dx;
area += smallArea( r1, r2, x );
}
return area*dx*8;
}

int main(){
int t;
double r1, r2, tmp;
double area;

cin >> t;
while( t--){
cin >> r1 >> r2;
if( r1 > r2 ){
tmp = r1;
r1 = r2;
r2 = tmp;
}
area = getArea( r1, r2 );
printf("%.2lf\n", area);
}

return 0;
}

1. 约瑟夫也用说这么长……很成熟的一个问题了，分治的方法解起来o(n)就可以了，有兴趣可以看看具体数学的第一章，关于约瑟夫问题推导出了一系列的结论，很漂亮

2. int half(int *array,int len,int key)
{
int l=0,r=len;
while(l<r)
{
int m=(l+r)>>1;
if(key>array )l=m+1;
else if(key<array )r=m;
else return m;
}
return -1;
}
这种就能避免一些Bug
l,m,r
左边是l,m;右边就是m+1,r;