南阳理工oj第3题

import java.util.Scanner;  
  
public class Main {  
    public static void main(String[] args) {  
        Scanner input = new Scanner(System.in);  
        int n = input.nextInt();  
        while (n-- > 0) {  
            int m = input.nextInt();  
            Shape[] arr = new Shape[m];  //对象数组
            for (int i = 0; i < m; i++) {  
                double x = input.nextDouble();  
                double y = input.nextDouble();  
                arr[i] = new Shape(x, y);  //每次都要new一个对象出来
            }  
            input.close();
            double area = 0.0;//多边形面积  
            double Gx = 0.0, Gy = 0.0;// 重心的x、y  
            for (int i = 1; i <= m; i++) {  
                double temp=(arr[i%m].x*arr[i-1].y-arr[i%m].y*arr[i-1].x)/2.0;  
                area += temp;  
                Gx += temp * (arr[i % m].x + arr[i - 1].x) / 3.0;  
                Gy += temp * (arr[i % m].y + arr[i - 1].y) / 3.0;  
            }  
            if (area - 0 < 0.0000001) {  
                System.out.println("0.000 0.000");  
                continue;  
            }  
            System.out.print(String.format("%.3f %.3f\n",area,(Gx + Gy)/area));  
        }  
    }  
}  
class Shape {  
    double x = 0;  
    double y = 0;  
    Shape(double x, double y) {  
        this.x = x;  
        this.y = y;  
    }  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值