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