题目大于:
平面上有N 条直线,其中第i 条直线是y = Ai * x + Bi。
请计算这些直线将平面分成了几个部分。
输入个数:
第一行包含一个整数N。
以下N 行,每行包含两个整数Ai, Bi。
对于50% 的评测用例,1 ≤ N ≤ 4, -10 ≤ Ai, Bi ≤ 10。
对于所有评测用例,1 ≤ N ≤ 1000, -100000 ≤ Ai, Bi ≤ 100000。
输出格式:
一个整数代表答案
分析:
第一种情况:
两条线重合了,直接continue
两条直线平行但不重合,平面被分割的面数 +1
第二种情况:
两条线相交,平面被分割的面数 + 交点个数 + 1
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1100;
typedef pair<double,double> PDD;
bool st[N];//标记每条线是否重边
double a[N][2];
int main()
{
int n;
cin >> n;
int ans = 1;//答案
for(int i=1;i<=n;i++)
{
cin >> a[i][0] >> a[i][1];
set<PDD> point;//记录交点
for(int j=1;j<=i-1;j++)
{
if(st[j]) continue;
if(a[i][0] == a[j][0])
{
if(a[i][1] == a[j][1])
{
st[i] = true;//重边
break;
}
else
continue;
}
double x = (a[i][1] - a[j][1]) / (a[j][0] - a[i][0]);
double y = a[i][0] * x + a[i][1];
point.insert({x,y});
}
if(!st[i]) ans += point.size() + 1;
}
cout << ans << endl;
return 0;
}