标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6971 Accepted Submission(s): 2919
package 数学题; import java.text.DecimalFormat; import java.util.Scanner; public class hdu_1115 { static class point { double x, y; point(double x, double y) { this.x = x; this.y = y; } } static point[] p; public static void main(String[] args) { DecimalFormat df= (DecimalFormat)DecimalFormat.getInstance(); df.applyPattern("0.00"); Scanner sc = new Scanner(System.in); int tcase = sc.nextInt(); while (tcase-- > 0) { int n = sc.nextInt(); p = new point[n]; for (int i = 0; i < n; i++) { double x = sc.nextDouble(); double y = sc.nextDouble(); p[i] = new point(x, y); } double s = 0,sum=0; double gx = 0,gy=0; for (int i = 1; i < n - 1; i++) { s = getArea(p[i], p[i + 1], p[0]); gx += s * (p[i].x + p[i + 1].x + p[0].x)/3; gy += s * (p[i].y + p[i + 1].y + p[0].y)/3; sum+=s; } double X = gx / sum; double Y =gy / sum; System.out.println(df.format(X)+" "+df.format(Y)); } } ///叉积除二得面积 private static double getArea(point p1, point p2, point p) { return ((p1.x - p.x) * (p2.y - p.y) - (p2.x - p.x) * (p1.y - p.y)) / 2; } }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5427384.html