为什么会RE?
原题链接:566.三点共线2023-07-17 11:16:08
发布于:广东
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
int gcd(int a, int b){
return b > 0 ? gcd(b, a%b) : a;
}
int countCollinearPoints(vector<pair<int, int>>& points) {
int n = points.size();
int count = 0;
unordered_map<int, int> slopeCount;
for (int i = 0; i < n; i++) {
slopeCount.clear();
for (int j = i+1; j < n; j++) {
int x1 = points[i].first;
int y1 = points[i].second;
int x2 = points[j].first;
int y2 = points[j].second;
int dx = x2 - x1;
int dy = y2 - y1;
int g = gcd(dx, dy);
dx /= g;
dy /= g;
int slope = dy * n + dx;
count += slopeCount[slope];
slopeCount[slope]++;
}
}
return count;
}
int main() {
int n;
cin >> n;
vector<pair<int, int>> points;
for (int i = 0; i < n; i++) {
int x, y;
cin >> x >> y;
points.push_back({x, y});
}
int ans = countCollinearPoints(points);
cout << ans << endl;
return 0;
}
全部评论 3
参考这个题解:https://www.acgo.cn/problemset/566/17029?tab=explanation
2024-04-25 来自
0可能减一个数就好了
2024-04-24 来自 广东
0RE是越界
2024-04-24 来自 广东
0
有帮助,赞一个