蓝桥 ADV-230 算法提高 12-1三角形 【数学公式】

  算法提高 12-1三角形  
时间限制:1.0s   内存限制:256.0MB
    
问题描述
  为二维空间中的点设计一个结构体,在此基础上为三角形设计一个结构体。分别设计独立的函数计算三角形的周长、面积、中心和重心。输入三个点,输出这三个点构成的三角形的周长、面积、外心和重心。结果保留小数点后2位数字。
样例输出
与上面的样例输入对应的输出。
例:
数据规模和约定
  输入数据中每一个数的范围。
  例:doule型表示数据。

题目链接:

  http://lx.lanqiao.cn/problem.page?gpid=T415

题目大意:

  给三角形三点坐标

  计算周长 面积 外心 重心

题目思路:

  【数学公式】

  带入数学公式即可。

  外心:
    x = △x/△, y = △y/△
    其中 △ = 2(xa-xb)(yc-yb) - 2(ya-yb)(xc-xb)
    △x = (yc-yb)(xa^2+ya^2-xb^2-yb^2) - (ya-yb)(xc^2+yc^2-xb^2-yb^2)
    △y = (xa-xb)(xc^2+yc^2-xb^2-yb^2) - (xc-xb)(xa^2+ya^2-xb^2-yb^2)

  重心:

    x=(xa+xb+xc)/3, y=(ya+yb+yc)/3

 1 /****************************************************
 2     
 3     Author : Coolxxx
 4     Copyright 2017 by Coolxxx. All rights reserved.
 5     BLOG : http://blog.csdn.net/u010568270
 6     
 7 ****************************************************/
 8 #include<bits/stdc++.h>
 9 #pragma comment(linker,"/STACK:1024000000,1024000000")
10 #define abs(a) ((a)>0?(a):(-(a)))
11 #define lowbit(a) (a&(-a))
12 #define sqr(a) ((a)*(a))
13 #define mem(a,b) memset(a,b,sizeof(a))
14 const double eps=1e-8;
15 const int J=10000;
16 const int mod=1000000007;
17 const int MAX=0x7f7f7f7f;
18 const double PI=3.14159265358979323;
19 const int N=1004;
20 using namespace std;
21 typedef long long LL;
22 double anss;
23 LL aans;
24 int cas,cass;
25 int n,m,lll,ans;
26 class point
27 {
28 public:
29     double x,y;
30 };
31 class triangle
32 {
33 public:
34     point a,b,c;
35     double aa,bb,cc,p;
36     triangle(point i,point j,point k)
37     {
38         a=i,b=j,c=k;
39         aa=sqrt(sqr(a.x-b.x)+sqr(a.y-b.y));
40         bb=sqrt(sqr(a.x-c.x)+sqr(a.y-c.y));
41         cc=sqrt(sqr(b.x-c.x)+sqr(b.y-c.y));
42         p=(aa+bb+cc)/2;
43     }
44     double perimeter()
45     {
46         return p+p;
47     }
48     double area()
49     {
50         return sqrt(p*(p-aa)*(p-bb)*(p-cc));
51     }
52     point circumcenter()
53     {
54         point p;
55         p.x=(c.y-b.y)*(sqr(a.x)+sqr(a.y)-sqr(b.x)-sqr(b.y))-(a.y-b.y)*(sqr(c.x)+sqr(c.y)-sqr(b.x)-sqr(b.y));
56         p.x/=2*(a.x-b.x)*(c.y-b.y)-2*(a.y-b.y)*(c.x-b.x);
57         p.y=(a.x-b.x)*(sqr(c.x)+sqr(c.y)-sqr(b.x)-sqr(b.y))-(c.x-b.x)*(sqr(a.x)+sqr(a.y)-sqr(b.x)-sqr(b.y));
58         p.y/=2*(a.x-b.x)*(c.y-b.y)-2*(a.y-b.y)*(c.x-b.x);
59         return p;
60     }
61     point centroid()
62     {
63         point p;
64         p.x=(a.x+b.x+c.x)/3;
65         p.y=(a.y+b.y+c.y)/3;
66         return p;
67     }
68 };
69 int main()
70 {
71     #ifndef ONLINE_JUDGE
72 //    freopen("1.txt","r",stdin);
73 //    freopen("2.txt","w",stdout);
74     #endif
75     int i,j,k,l;
76     int x,y,z;
77 //    for(scanf("%d",&cass);cass;cass--)
78 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
79 //    while(~scanf("%s",s))
80 //    while(~scanf("%d",&n))
81     if(1)
82     {
83         point a,b,c;
84         cin>>a.x>>a.y>>b.x>>b.y>>c.x>>c.y;
85         triangle p(a,b,c);
86         a=p.circumcenter();
87         b=p.centroid();
88         printf("%.2lf
",p.perimeter());
89         printf("%.2lf
",p.area());
90         printf("%.2lf %.2lf
",a.x,a.y);
91         printf("%.2lf %.2lf
",b.x,b.y);
92     }
93     return 0;
94 }
95 /*
96 //
97 
98 //
99 */
View Code
原文地址:https://www.cnblogs.com/Coolxxx/p/6671500.html