SaySomething

Talk is cheap, Show me the code.

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

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));
        }
    }
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/no_alternantive/article/details/51543507
文章标签: hdu 水题 数学
个人分类: HDU 水题 数论
上一篇CSU 国防科大决赛 D题 找路径 (水题-DP)
下一篇ACM 用Java编写基础输入输出问题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭