poj 3907

Description

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.
Input

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).
Output

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.)
Sample Input

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
Sample Output

0
2
3
循环求三角形的面积和
最后关于强制转换,好像不同的编译器,有不同的结果大哭大哭大哭

#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
double hehe(double x1,double y1,double x2,double y2)
{
   return x1*y2-x2*y1;
}
int main()
{
    int n;
    double sum;
    while(cin>>n&&n!=0)
    {
     sum=0;
     double a[n],b[n];
     for(int i=0;i<=n-1;i++)
     {
      cin>>a[i]>>b[i];
     }
     for(int i=1;i<=n;i++)
     {
       if(i<n)
      sum=sum+hehe(a[i-1],b[i-1],a[i],b[i]);
       else
       sum=sum+hehe(a[i-1],b[i-1],a[0],b[0]);
     }
    cout <<(int)fabs(sum)/2<< endl;
    }
    return 0;
}

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值