WOJ1402-Build Your Home

Mr. Tenant is going to buy a new house. In fact, he is going to buy a piece of land and build his new house on it. In order to decide which piece of land to buy, Mr. Tenant needs a program which will give a score to each piece. Each candidate piece of land is a polygonal shape (not necessarily convex), and Mr.  Tenant wonders what the best score is. Among possible scores, he considered the number of vertices, sum of angles, minimum number of required guards, and so forth. Finally, Mr. Tenant decided that the best score for a piece of land would simply be its area. Your task is to write the desired scoring program.

输入格式

The input file consists of multiple pieces of land. Each piece is a simple polygon (that is, a polygon which does not intersect itself). A polygon description starts with a positive integer number k, followed by k vertices, where each vertex consists of two coordinates (floating-point numbers): x and y. Naturally, the last vertex is connected by an edge to the first vertex. Note that each polygon can be ordered either clockwise or counterclockwise. The input ends with a "0" (the number zero).

输出格式

For each piece of land, the output should consist of exactly one line containing the score of that piece, rounded to the nearest integer number. (Halves should be rounded up, but Mr. Tenant never faced such cases.) Hint: The scoring program has to handle well degenerate cases, such as, polygons with only one or two vertices.

样例输入

1 123.45 67.890
3 0.001 0 1.999 0 0 2
5 10 10 10 12 11 11 12 12 12.0 10.0
0

样例输出

0
2
3


#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
struct point{ 
    double x,y;
}poly[1000];
int main(){
    int n,i;
    while(scanf("%d",&n)&&n){
        double res=0;
        for(i=0;i<n;i++)
            scanf("%lf %lf",&poly[i].x,&poly[i].y);
        for(i=0;i<n;i++){
            res+=poly[i].x*poly[(i+1)%n].y;
            res-=poly[i].y*poly[(i+1)%n].x;
        }
        printf("%.lf\n",fabs(res/2.0));
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
//注意将顶点按逆时针排序
int main() {
	int n,i,j;
	while(1) {
		scanf("%d",&n);
		if(n==0)
			break;

		double a[n][2];
		double sum=0.0;
		for(i=0; i<n; i++)
			for(j=0; j<2; j++)
				scanf("%lf",&a[i][j]);
		if(n==1||n==2) {
			printf("0\n");
			continue;
		}
		for(i=0; i<n-1; i++)
			sum+=(a[i][0]*a[i+1][1]-a[i+1][0]*a[i][1])/2.0;
		sum+=(a[n-1][0]*a[0][1]-a[0][0]*a[n-1][1])/2.0;
		sum=fabs(sum);
		printf("%d\n",(int)(sum+0.5));
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值