zoukankan      html  css  js  c++  java
  • Triangle

    Problem Description
    This is a simple question, give you a triangle, please divide the triangle into two parts with a segment. These two parts should have the same area, and the segment should be as short as possible. Please calculate the length of the segment.
     

    Input
    The first line of the input is the number T(T<=100), which is the number of cases followed. For each test case, the only line contains space-separated integers x1, y1, x2, y2, x3, y3,the coordinates of triangle endpoint. The absolute value of all the coordinates will not be more than 109.
     

    Output
    For each test case, output only one line, containing the length of segment only. Your answer should be rounded to 4 digits after the decimal point.
     

    Sample Input
    1 0 0 10 0 0 10
     

    Sample Output
    6.4359

    因为要分割为两个面积相等的两部分,而且要使分割线最小,所以选择最小顶点多对应的分割线,这样分割线会最短。而且分割后得到的三角形是等腰三角形。
    先找出最小边,然后把其对应的角的cos值算出来,然后再算出等腰三角形的腰,最后算出分割线的长。
    #include<stdio.h>
    #include<math.h>
    double f1,f2,a1,a2,a3,b1,b2,b3;
    int cas;
    int main(){
        scanf("%d",&cas);
        int casno=0;
        while(cas--){
            scanf("%lf%lf%lf%lf%lf%lf",&a1,&b1,&a2,&b2,&a3,&b3);
            double a=sqrt((a1-a2)*(a1-a2)+(b1-b2)*(b1-b2));
            double b=sqrt((a1-a3)*(a1-a3)+(b1-b3)*(b1-b3));
            double c=sqrt((a3-a2)*(a3-a2)+(b3-b2)*(b3-b2));
            double th;
            if(a<=b&&a<=c){
                th=acos(-(a*a-b*b-c*c)/2/b/c);
                f1=b,f2=c;
            }else if(b<=a&&b<=c){
                th=acos(-(b*b-a*a-c*c)/2/a/c);
                f1=a,f2=c;
            }else {
                th=acos(-(c*c-a*a-b*b)/2/a/b);
                f1=a,f2=b;
            }
            //printf("%.4f %.4f %.4f
    ",a,b,c);
            //printf("%.4f %.4f %.4f
    ",th,f1,f2);        
            double x=sqrt(f1*f2/2);        
            printf("%.4f
    ",2*sin(th/2)*x);
        }
    }

  • 相关阅读:
    hdu4405Aeroplane chess 概率dp水题
    【基础算法】排序-复杂排序之二(找出第K大的数)
    java代码实现输出指定以.java结尾的文件的绝对路径
    算法 Heap sort
    hdu2141Can you find it?
    c/c++:回调函数
    怎样在编译的时候,控制删除apk不用的资源?
    javascript原型的改动与重写(覆盖)区别
    深入理解Tomcat虚拟文件夹
    C++设计模式之代理模式
  • 原文地址:https://www.cnblogs.com/herumw/p/9464864.html
Copyright © 2011-2022 走看看