文章

27

粉丝

86

获赞

10

访问

21.0k

头像
多边形的面积 题解:C++ 鞋带公式
P1615
发布于2023年8月24日 13:42
阅读数 601

C++

S三角形=0.5∗((x1∗y2+x2∗y3+x3∗y1)−(y1∗x2+y2∗x3+y3∗x1))

我们代个数进去试试 A:(0, 4) , B:(0, 0) , C:(3, 0) 

S三角形=0.5∗((0∗0+0∗0+3∗4)−(4∗0+0∗3+0∗0))=6

在这里插入图片描述

#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct node{
    int x;
    int y;
};
int main() {
    int N;
    cin >> N;
    int m, n;
    vector<node> a(N);
    for(int i = 0; i < N; i++){
        cin >> m >> n;
        a[i].x = m;
        a[i].y = n;
    }
    int res = 0;
    for(int i = 0; i < N - 1; i++){
        res += a[i].x * a[i + 1].y;
    }
    res += a[N - 1].x * a[0].y;
    for(int i = 0; i < N - 1; i++){
        res -= a[i].y * a[i + 1].x;
    }
    res -= a[N - 1].y * a[0].x;
    res = abs(res / 2);
    cout << res << endl;
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发