You are given two set of points. The first set is determined by the equation A1x + B1y + C1 = 0, and the second one is determined by the equation A2x + B2y + C2 = 0.
Write the program which finds the number of points in the intersection of two given sets.
The first line of the input contains three integer numbers A1, B1, C1 separated by space. The second line contains three integer numbers A2, B2, C2 separated by space. All the numbers are between -100 and 100, inclusive.
Print the number of points in the intersection or -1 if there are infinite number of points.
1 1 0 2 2 0
-1
1 1 0 2 -2 0
1
反思:一定要先判断特殊情况!
1. 题目说的是两个点集。
2. 优先判断特殊情况——某一集合是空集或(该题定义下的)全集。
3. 排除上述情况后,判断两个点集是否等价即可。
完整代码:
/*15ms,0KB*/
#include<cstdio>
using namespace std;
int main()
{
int a, b, c, A, B, C;
scanf("%d%d%d%d%d%d", &a, &b, &c, &A, &B, &C);
if ((a == 0 && b == 0) || (A == 0 && B == 0))
{
if ((a == 0 && b == 0 && c != 0) || (A == 0 && B == 0 && C != 0))
printf("0");///空集
else
printf("-1");///全集
}
else if (a * B == A * b)
{///看两个点集是否等价,注意当x前系数为0时改用y前系数判断
if (a)
printf("%d", a * C != A * c ? 0 : -1);
else
printf("%d", b * C != B * c ? 0 : -1);
}
else
printf("1");
return 0;
}
解析代码片段:两组点集交集数量计算

本文详细解析了一个代码片段,用于计算由两条直线方程定义的两组点集的交集数量。包括输入参数解析、特殊情况处理、等价点集判断及最终输出结果的实现。
533

被折叠的 条评论
为什么被折叠?



