NYOJ-3 多边形重心问题

NYOJ-3 多边形重心问题

解题思路么,主要就是向量X乘求面积咯,

随便百度一下一大把,也容易理解,

说一下坑了我蛮久的问题,实在不吐不快

精度!精度!精度!

重要的事情说三遍!

好了,代码如下,

知道解题思路的话,理解代码还是很容易的,

如果感觉自己的思路没问题,那么可以参考一下问题大概在哪,,

提供两种思路吧,实则大同小异

方法一:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
    static class node{
        public node(double x, double y) {
            this.x=x;
            this.y=y;
        }
        double x;
        double y;
    }

    public static void main(String[] args) {
        DecimalFormat decimalFormat=new DecimalFormat("0.000");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m;
        node tempnode1;
        node tempnode2;
        node tempnode3;
        double area=0;
        double areatemp=0;
        double mid=0;
        for(int i=0;i<n;++i){
            mid=0;
            area=0;
            m=sc.nextInt();
            tempnode1=new node(sc.nextDouble(), sc.nextDouble());
            tempnode2=new node(sc.nextDouble(), sc.nextDouble());
            for(int j=2;j<m;++j){
                tempnode3=new node(sc.nextDouble(), sc.nextDouble());
                areatemp=((tempnode3.y-tempnode1.y)*(tempnode2.x-tempnode1.x)-
                        (tempnode3.x-tempnode1.x)*(tempnode2.y-tempnode1.y) )/2;
                area+=areatemp;
                mid+=(tempnode1.x+tempnode1.y+tempnode2.x+tempnode2.y+tempnode3.x+tempnode3.y)/3*areatemp;
                tempnode2=tempnode3;
            }
            if(area!=0)
                mid/=area;
            else
                mid=0;
            area=Math.abs(area);
            System.out.println(decimalFormat.format(area)+" "+decimalFormat.format(mid));
        }
        sc.close();
    }
}

方法二:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        DecimalFormat decimalFormat=new DecimalFormat("0.000");
        Scanner sc=new Scanner(System.in);
        int n=sc.nextInt();
        int m;
        double area=0;
        double areatemp=0;
        double x1, y1, tempx, tempy, tempx1, tempy1 ;
        double mid=0;
        for(int i=0;i<n;++i){
            area=0;
            mid=0;
            m=sc.nextInt();
            x1=sc.nextDouble();
            y1=sc.nextDouble();
            tempx1=x1;
            tempy1=y1;
            for(int j=1;j<m;++j){
                tempx=sc.nextDouble();
                tempy=sc.nextDouble();
                areatemp=(tempx*tempy1-tempy*tempx1)/2;
                area+=areatemp;
                mid+=areatemp*(tempx+tempy+tempx1+tempy1)/3;
                tempx1=tempx;
                tempy1=tempy;
            }
            areatemp=(x1*tempy1-y1*tempx1)/2;
            area+=areatemp;
            mid+=areatemp*(tempx1+tempy1+x1+y1)/3;
            if(area>=0.001)
                mid/=area;
            else
                mid=0;
            area=Math.abs(area);
            System.out.println(decimalFormat.format(area)+" "+decimalFormat.format(mid));
        }
        sc.close();
    }
}       


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值