Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3971 Accepted Submission(s): 2280
1 #include <iostream>
2 #include <stdio.h>
3 using namespace std; 4 /********** 定义点 **********/
5 struct Point{ 6 double x,y; 7 Point(double x=0,double y=0):x(x),y(y) {} 8 }; 9 /********** 定义向量 **********/
10 typedef Point Vector; 11
12 /********** 向量 + 向量 = 向量 **********/
13 Vector operator + (Vector a,Vector b) 14 { 15 return Vector(a.x+b.x,a.y+b.y); 16 } 17 /********** 点 - 点 = 向量 **********/
18 Vector operator - (Point a,Point b) 19 { 20 return Vector(a.x-b.x,a.y-b.y); 21 } 22 /********** 向量 * 数 = 向量 **********/
23 Vector operator * (Vector a,double b) 24 { 25 return Vector(a.x*b,a.y*b); 26 } 27 /********** 向量 / 数 = 向量 **********/
28 Vector operator / (Vector a,double b) 29 { 30 return Vector(a.x/b,a.y/b); 31 } 32 /********** 向量点积 **********/
33 double Dot(Vector a,Vector b) 34 { 35 return a.x*b.x+a.y*b.y; 36 } 37 /********** 2向量求叉积 **********/
38 double Cross(Vector a,Vector b) 39 { 40 return a.x*b.y-b.x*a.y; 41 } 42 /********** 3点求叉积 **********/
43 double Cross(Point a,Point b,Point c) 44 { 45 return (c.x-a.x)*(b.y-a.y) - (c.y-a.y)*(b.x-a.x); 46 } 47 Point GetLineIntersection(Point P,Vector v,Point Q,Vector w) //求射线交点
48 { 49 Vector u = P-Q; 50 double t = Cross(w,u) / Cross(v,w); 51 return P+v*t; 52 } 53 Point GetGravity(Point p[]) //求三角形重心
54 { 55 Vector v,w; 56 Point c1,c2; 57 c1.x = (p[0].x+p[2].x)/2; 58 c1.y = (p[0].y+p[2].y)/2; 59 c2.x = (p[1].x+p[2].x)/2; 60 c2.y = (p[1].y+p[2].y)/2; 61 v = c1-p[1]; 62 w = c2-p[0]; 63 return GetLineIntersection(p[1],v,p[0],w); 64 } 65 int main() 66 { 67 int n; 68 while(cin>>n){ 69 if(n==0) break; 70 for(int i=0;i<n;i++){ //输入
71 Point p[3]; 72 cin>>p[0].x>>p[0].y; 73 cin>>p[1].x>>p[1].y; 74 cin>>p[2].x>>p[2].y; 75 Point r = GetGravity(p); 76 printf("%.1lf %.1lf\n",r.x,r.y); 77 } 78 } 79 return 0; 80 }
代码二:
1 #include <stdio.h>
2 typedef struct { 3 double x,y; 4 }Point; 5 int main() 6 { 7 int n; 8 while(scanf("%d",&n)!=EOF){ 9 if(n==0) break; 10 while(n--){ 11 Point a,b,c; 12 scanf("%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y); 13 Point z; 14 z.x = (a.x+b.x+c.x)/3; 15 z.y = (a.y+b.y+c.y)/3; 16 printf("%.1lf %.1lf\n",z.x,z.y); 17 } 18 } 19 return 0; 20 }
Freecode : www.cnblogs.com/yym2013