zoukankan      html  css  js  c++  java
  • sdnu 1334 Jason's Water Problem【计算几何-多边形面积的计算】【负数取模】

    1334.Jason's Water Problem

    Time Limit: 1000 MS    Memory Limit: 32768 KB
    Total Submission(s): 128    Accepted Submission(s): 23

    Description

    Calculus is the first lesson Jason had. It is tedious, so an interesting idea occurred to Jason. He took out his mobile phone and open the HDUOJ and found a water problem. He said to himself, “I must solve this f**king easy water problem in ten minutes! ”.

    You were invited to compete with him online. Don’t be defeated.

    Input

    Multiple test cases, Each test first contains an integer N(3<=N<=100000) indicating the number of vertices of the polygon. The i-th of the following Nlines contains two integers xi and yi (0<=xi,yi<=10^9) separated by a blank. (xi,yi) is the i-th vertex of the polygon, and (x1,y1),...,(xn,yn) will be in counterclockwise order.

    Output

    For each test case, output a number as the answer which is the area of the polygon be multiplied by 2. In case the answer is greater than 1000000006, please modulo the answer with 1000000007.

    Sample Input

    3
    0 0
    1 0
    0 1
    4
    0 0
    1 0
    1 1
    0 1

    Sample Output

    1
    2

    Hint

     

    Source

    错的代码:
     1 #include<bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 const ll mod=1000000007;
     5 struct point
     6 {
     7     ll x,y;
     8 }a[100050];
     9 ll calc(point c,point d)
    10 {
    11     return c.x*d.y-d.x*c.y;
    12 }
    13 int main()
    14 {
    15     int n;
    16     while(scanf("%d",&n)!=EOF)
    17     {
    18         for(int i=1;i<=n;i++)
    19         {
    20             scanf("%lld %lld",&a[i].x,&a[i].y);
    21         }
    22         ll ans=0;
    23         a[n+1]=a[1];
    24         for(int i=2;i<=n+1;i++)
    25         {
    26             ans=(ans+calc(a[i-1],a[i])%mod)%mod;
    27         }
    28         cout<<ans<<'
    ';
    29     }
    30 }

    ac的代码:

     1 #include<bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 const ll mod=1000000007;
     5 struct point
     6 {
     7     ll x,y;
     8 }a[100050];
     9 ll calc(point c,point d)
    10 {
    11     return (c.x*d.y-d.x*c.y);
    12 }
    13 int main()
    14 {
    15     int n;
    16     while(scanf("%d",&n)!=EOF)
    17     {
    18         for(int i=1;i<=n;i++)
    19         {
    20             scanf("%lld %lld",&a[i].x,&a[i].y);
    21         }
    22         a[n+1]=a[1];
    23         ll ans=0;
    24         for(int i=2;i<=n+1;i++)
    25         {
    26             ans=(ans+calc(a[i-1],a[i])%mod+mod)%mod;
    27         }
    28         cout<<ans<<'
    ';
    29     }
    30 }

    负数取余的公式为 (a%mod+mod)%mod  (orz好笨啊)

    原引某博客的图

    多边形的面积计算,多边形每个边两个端点跟原点组成一系列三角形,它们的面积有正负,所面积之和就是多边形的面积。 
    这里写图片描述

    这里选取的端点p为原点(逃)

  • 相关阅读:
    RESTful API 设计指南
    SpringBoot系列
    spring boot RESTFul
    mark--唧唧歪歪--2021.2.8
    接口踩坑:Status (blocked:other)
    获取layer.open弹出层的返回值
    VUE_CLI4.5 VUE+Cesium
    Android Studio AVD 模拟器 自带虚拟机 联网 连接网络
    一文搞懂TCP与UDP的区别
    springboot打包成war,部署到tomcat无法访问的问题
  • 原文地址:https://www.cnblogs.com/guanwen769aaaa/p/11281240.html
Copyright © 2011-2022 走看看