原题传送门
跟实数运算有关的题目做起来总是很方。。。
就是求多少条直线跟线段有交点
针对每条直线求出解析式
a
x
+
b
y
+
c
=
0
−
−
>
y
=
−
a
b
x
−
c
b
ax+by+c=0-->y=-\frac{a}{b}x-\frac{c}{b}
ax+by+c=0−−>y=−bax−bc
x
1
,
x
2
x1,x2
x1,x2代进去,只要两个对应值和
y
1
,
y
2
y1,y2
y1,y2大小关系不一样即说明有交点
特判
b
=
0
b=0
b=0的情况,此时直线方程是
x
=
−
c
a
x=-\frac{c}{a}
x=−ac
此时判断
x
x
x是否∈
[
x
1
,
x
2
]
(
x
1
<
x
2
)
[x1,x2](x1<x2)
[x1,x2](x1<x2)即可
Code:
#include <bits/stdc++.h>
using namespace std;
int x1, x2, Y1, y2, n;
inline int read(){
int s = 0, w = 1;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
return s * w;
}
int main(){
x1 = read(), Y1 = read(), x2 = read(), y2 = read(), n = read();
if (x1 > x2) swap(x1, x2), swap(Y1, y2);
int ans = 0;
while (n--){
int a = read(), b = read(), c = read();
if (!b){
double x = 1.0 * (-c) / a;
ans += (x1 <= x) && (x2 >= x) ;
} else{
double k = 1.0 * (-a) / b, B = 1.0 * (-c) / b;
double fx1 = k * x1 + B, fx2 = k * x2 + B;
ans += (fx1 > Y1) != (fx2 > y2);
}
}
printf("%d\n", ans);
return 0;
}