zoukankan      html  css  js  c++  java
  • 叉积求多变形面积

    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

    Hint

    The scoring program has to handle well degenerate cases, such as, polygons with only one or two vertices.
     
    #include <iostream>
    #include <cmath>
    #include <stdio.h>
    using namespace std;
    int main()
    {
        float x0,y0,x1,y1;
        int n;
        while(scanf("%d",&n)!=-1)
        {
            if(n==0)
                break;
            float sum=0;
            scanf("%f%f",&x0,&y0);
            float xx=x0,yy=y0;
            n--;
            while(n--)
            {
                scanf("%f%f",&x1,&y1);
                sum+=x0*y1-x1*y0;
                x0=x1;
                y0=y1;
            }
            x1=xx;
            y1=yy;
            sum+=x0*y1-x1*y0;
            printf("%.0f
    ",fabs(sum)/2+1e-6);
        }
        return 0;
    }
  • 相关阅读:
    .NET 分布式自增Id组件(解决自动分配机器Id、时间回拨问题)
    简洁实用Socket框架DotNettySocket
    Colder框架硬核更新(Sharding+IOC)
    .NET Core开源快速开发框架Colder发布 (NET Core2.1+AdminLTE版)
    .NET开源快速开发框架Colder发布 (NET452+AdminLTE版)
    .NET 跨平台RPC框架DotNettyRPC
    Web后台快速开发框架(.NET Core)
    Web后台快速开发框架
    EasyWcf------无需配置,无需引用,动态绑定,轻松使用
    C# .NET 0配置使用Wcf(半成品)
  • 原文地址:https://www.cnblogs.com/chen9510/p/4703010.html
Copyright © 2011-2022 走看看