NYOJ-3 多边形重心问题
来源:互联网 发布:阿里云备案多长时间 编辑:程序博客网 时间:2024/05/22 01:51
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
- nyoj 3 多边形重心问题
- nyoj 3 多边形重心问题
- nyoj-3-多边形重心问题
- nyoj-3-多边形重心问题
- NYOJ--3--多边形重心问题
- nyoj 3 多边形重心问题
- nyoj 3 多边形重心问题
- nyoj 3 多边形重心问题
- NYOJ-3 多边形重心问题
- NYOJ 3:多边形重心问题
- NYOJ 3 多边形重心问题
- NYOJ-----3---多边形重心问题
- NYOJ多边形重心问题
- NYOJ - 多边形重心问题
- NYOJ-3 多边形重心问题【计算几何】
- nyoj--3-多边形重心问题(计算几何)
- NYOJ 题目3 多边形重心问题
- NYOJ 3 多边形重心问题 【计算几何】
- mysql日志详细解析
- javascript的同源策略
- 静态分析
- Android 6.0 申请权限Permission Denial: reading com.android.providers.media.MediaProvider
- 程序猿的情书
- NYOJ-3 多边形重心问题
- android 自定义PopupWindow,注意的问题
- 新版MATERIAL DESIGN 官方动效指南(三)
- APP | 001-Android.APP——JavaWeb后台
- Sqoop的那些事
- post dynamic
- c++里面的switch-case问题
- 被 7 整除的数
- 第一章 事件与概率