题目描述
在平面直角坐标系中,两点可以确定一条直线。
给定平面上 20 × 21 20 × 21 20×21个整点 ( x , y ) ∣ 0 ≤ x < 20 , 0 ≤ y < 21 , x ∈ Z , y ∈ Z {(x, y)|0 ≤ x < 20, 0 ≤ y < 21, x ∈ Z, y ∈ Z} (x,y)∣0≤x<20,0≤y<21,x∈Z,y∈Z,即横坐标是 0 到 19 0 到 19 0到19 (包含 0 0 0 和 19 19 19) 之间的整数、纵坐标是 0 0 0 到 20 20 20 (包含 0 0 0 和 20 20 20) 之 间的整数的点。
请问这些点一共确定了多少条不同的直线。
C++
#include <iostream>
#include <cstdio>
#include <set>
#define x first
#define y second
using namespace std;
typedef pair<double, double> PDD;
set<PDD> S;
PDD point[500];
int cnt;
int main() {
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 21; j++) {
point[cnt] = {i, j};
cnt++;
}
}
int ans = 20 + 21;
for (int i = 0; i < cnt; i++) {
for (int j = 0; j < cnt; j++) {
double x1 = point[i].x, y1 = point[i].y;
double x2 = point[j].x, y2 = point[j].y;
if (x1 == x2 || y1 == y2) continue;
double k = (y2-y1)/(x2-x1);
double b = (x2*y1-x1*y2) / (x2-x1);
S.insert({k, b});
}
}
ans += S.size();
cout << ans;
return 0;
}