题意:。。
这道题跟HDOJ 1115 差不多但是需要判断是不是一条线段.
代码:
#include<stdio.h>
#include<math.h>
#define MAXN 10005
struct node{
double x, y;
}s[MAXN];
double area(node p1, node p2, node p3)
{
return (p1.x-p2.x)*(p3.y-p2.y) - (p3.x-p2.x)*(p1.y-p2.y);
}
int main()
{
int t, n, i, j;
scanf("%d", &t);
while(t --){
scanf("%d", &n);
for(i = 0; i < n; i ++){
scanf("%lf%lf", &s[i].x, &s[i].y);
}
double sum_x, sum_y, sum_area, temp;
sum_x = sum_y= sum_area = 0;
for(i = 1; i < n-1; i ++){
temp = area(s[0], s[i], s[i+1])/2; //这里不知道为什么不用乘-1了
sum_x += temp*(s[0].x+s[i].x+s[i+1].x)/3;
sum_y+=temp*(s[0].y+s[i].y+s[i+1].y)/3;
sum_area += temp;
}
if(fabs(sum_area) < 0.000001){
printf("0.000 0.000\n");
continue;
}
printf("%.3lf %.3lf\n", sum_area, (sum_y+sum_x)/sum_area);
}
return 0;
}
题目链接:
http://acm.nyist.net/JudgeOnline/problem.php?pid=3