5218: The Mountain

传送门

5218: The Mountain

时间限制: 1 Sec  内存限制: 128 MB
提交: 106  解决: 92
[提交] [状态] [讨论版] [命题人:admin]

题目描述

All as we know, a mountain is a large landform that stretches above the surrounding land in a limited area. If we as the tourists take a picture of a distant mountain and print it out, the image on the surface of paper will be in the shape of a particular polygon.
From mathematics angle we can describe the range of the mountain in the picture as a list of distinct points, denoted by (x1 , y1 ) to (xn , yn ). The first point is at the original point of the coordinate system and the last point is lying on the x-axis. All points else have positive y coordinates and incremental x coordinates. Specifically, all x coordinates satisfy 0 = x1 < x2 < x3 <... < xn . All y coordinates are positive except the first and the last points whose y coordinates are zeroes.
The range of the mountain is the polygon whose boundary passes through points (x1 , y1 ) to (xn , yn ) in turn and goes back to the first point. In this problem, your task is to calculate the area of the range of a mountain in the picture.

 

输入

The input has several test cases and the first line describes an integer t (1 ≤ t ≤ 20) which is the total number of cases.
In each case, the first line provides the integer n (1 ≤ n ≤ 100) which is the number of points used to describe the range of a mountain. Following n lines describe all points and the i-th line contains two integers xi and yi (0 ≤ xi , yi ≤ 1000) indicating the coordinate of the i-th point.

 

输出

For each test case, output the area in a line with the precision of 6 digits.

 

样例输入

3
3
0 0
1 1
2 0
4
0 0
5 10
10 15
15 0
5
0 0
3 7
7 2
9 10
13 0

 

样例输出

1.000000
125.000000
60.500000

题意:给你n个点,保证第一个点为原点,最后一个点在x轴上,求这些点和x轴所围成的区域面积

分析:除第一个和最后一个为三角形外,其余全为梯形

代码如下

#include <bits/stdc++.h>
#define pre(i,x,n) for(int i=x;i<=n;i++)
#define rep(i,n,x) for(int i=n;i>=x;i--)
#define ll long long
using namespace std;
struct node
{
   double x,y;
}p[100010];
int main()
{
     int t,n;
     scanf("%d",&t);
     while(t--)
     {
        scanf("%d",&n);
        double ans=0;
        pre(i,1,n)
        {
            scanf("%lf%lf",&p[i].x,&p[i].y);
            if(i==1)
               ans+=p[i].x*p[i].y/2.0;
            else if(i==n)
               ans+=p[i-1].y*(p[i].x-p[i-1].x)/2.0;
            else
               ans+=(p[i].y+p[i-1].y)*(p[i].x-p[i-1].x)/2.0;
        }
        printf("%.6lf\n",ans);
     }
     return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2020/3/16

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值