HDU-1798 Tell me the area (C++和Java【水】两圆相交求公共面积)

来源:互联网 发布:oracle自带表数据脚本 编辑:程序博客网 时间:2024/05/19 13:27

HDU-1798 Tell me the area(两圆相交求公共面积)

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description
There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.

Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.

Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.

Sample Input
0 0 2
2 2 1

Sample Output
0.108

分析:题意就是求两圆的公共面积,简单数学题。
WA的情况:
1、PI不能写3.1415926,最好写acos(-1.0)
2、考虑情况(相切、相离、考虑等于0、包含 )

#include "cstdio"#include "cmath"#include "iostream" using namespace std;#define PI acos(-1.0)double dis(double x1,double y1,double x2,double y2){    return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));}double cos_angle(double a,double b,double c){    return 0.5*(a*a+c*c-b*b)/(a*c);}int main(){    double d,x1,y1,r1,x2,y2,r2,s,s1_,s2_,s1_tri,s2_tri,an1,an2;    while(~scanf("%lf %lf %lf",&x1,&y1,&r1)){        scanf("%lf %lf %lf",&x2,&y2,&r2);        d = dis(x1,y1,x2,y2);        if(d>=r1+r2||!r1||!r2){//相切、相离、考虑等于0             printf("0.000\n");            continue;        }        if(d+r1<=r2){//包含             printf("%.3lf\n",PI*r1*r1);            continue;        }        if(d+r2<=r1){//包含             printf("%.3lf\n",PI*r2*r2);            continue;        }        an1 = acos(cos_angle(r1,r2,d));        an2 = acos(cos_angle(r2,r1,d));        s1_tri = 0.5*r1*r1*sin(an1*2);        s2_tri = 0.5*r2*r2*sin(an2*2);        s1_ = r1*r1*an1;        s2_ = r2*r2*an2;        s = (s1_-s1_tri)+(s2_-s2_tri);        printf("%.3lf\n",s);    }    return 0;}

Java(S三角形 用海伦公式)
第一次用JAVA写ACM,解决输入输出问题。

import java.io.*;import java.math.*;import java.util.*;import java.text.*;public class Main {    public static void main(String []args){        Scanner sc = new Scanner(System.in);        while(sc.hasNext()){            double PI = Math.acos(-1.0);            double x1 = sc.nextDouble(); // 圆心x            double y1 = sc.nextDouble(); // 圆心y            double r1 = sc.nextDouble();             double x2 = sc.nextDouble();            double y2 = sc.nextDouble();            double r2 = sc.nextDouble();                    double alpha,area;            double d = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));//两圆心距离            if(d>=r1+r2||r1==0||r2==0){//相切、相离、考虑等于0                 area=0;            }            else if(d+r1<=r2){//包含                 area = PI*r1*r1;            }            else if(d+r2<=r1){//包含                 area = PI*r2*r2;            }            else{                alpha=Math.acos((d*d+r1*r1-r2*r2)/(2*d*r1));//余弦定理取得相交弧所对本圆的圆心角                area=alpha*r1*r1;//本圆扇形面积                alpha=Math.acos((d*d+r2*r2-r1*r1)/(2*d*r2));//余弦定理取得相交弧所对另一圆的圆心角                area+=alpha*r2*r2;//另一圆的扇形面积                double s=(d+r1+r2)/2;//海伦公式之s                area-=Math.sqrt(s*(s-d)*(s-r1)*(s-r2))*2;//两扇形面积减去两三角形面积即为交集            }            DecimalFormat df = new DecimalFormat( "0.000");             System.out.println(df.format(area));        }    }}
0 0