问题:
多边形面积:s=2/1|x1*y2-x2*y1+x2*y3-x3*y2+......xn*y1-x1*yn|
输出控制小数和空行格式:System.out.printf("%.1f\r\n",s);
3 0 0 1 0 0 1 4 1 0 0 1 -1 0 0 -1 0
0.5 2.0
代码:
import java.util.*; public class Main{ public static void main(String args[]){ Scanner cin=new Scanner(System.in); while(cin.hasNext()){ int n=cin.nextInt(); if(n==0) break; int[] a=new int[2*n]; float s=0; for(int i=0;i<2*n;i++) a[i]=cin.nextInt(); for(int i=0;i<2*n;i+=2){ if(i<2*n-2) s=s+a[i]*a[i+3]-a[i+1]*a[i+2]; else s=s+a[i]*a[1]-a[i+1]*a[0]; } s=s/2; if(s<0) s=-s; System.out.printf("%.1f\r\n",s); } } }
原文地址:http://blog.csdn.net/amx50b/article/details/46120205