给定n个点及xy坐标求出用任意三个点围成的三角形面积总和保留1位小数 n=3000;xiyi∈-1000010000例:输入:50 11 01 12 11 2输出:60C++实现
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double getTriangleArea(double x1, double y1, double x2, double y2, double x3, double y3) {
return abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2)) / 2.0;
}
double getSumOfTriangleAreas(vector<pair<double, double>>& points) {
double sum = 0.0;
int n = points.size();
for (int i = 0; i < n - 2; i++) {
for (int j = i + 1; j < n - 1; j++) {
for (int k = j + 1; k < n; k++) {
double x1 = points[i].first;
double y1 = points[i].second;
double x2 = points[j].first;
double y2 = points[j].second;
double x3 = points[k].first;
double y3 = points[k].second;
sum += getTriangleArea(x1, y1, x2, y2, x3, y3);
}
}
}
return sum;
}
int main() {
int n;
cin >> n;
vector<pair<double, double>> points(n);
for (int i = 0; i < n; i++) {
cin >> points[i].first >> points[i].second;
}
double sum = getSumOfTriangleAreas(points);
cout << fixed << sum << endl;
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/h450 著作权归作者所有。请勿转载和采集!