输入:
3
1 1
2 2
3 3
输出:
6
大三的哥哥问的一道题,所以就做了一下,也确实很累了,今天,起来后将注意事项详细写一下.
题目分析:
代码如下:
#include <iostream>
#include <set>
using namespace std;
const int maxn = 1010;
struct node {
long double x;
long double y;
bool operator<(const node& a)const {
return a.x>x;//运算符重载
}
};
set<node>s;//去重,自动排序
set<node>v;
long double a[maxn], b[maxn];
long long cnt = 2;//只有一条直线时候,两个平面
int n;
int main()
{
int aa, bb;
cin >> n;
for (int i = 0;i < n;i++) {
node p;
cin >> aa >> bb;
p.x = aa;
p.y = bb;
s.insert(p);
}
int i = 0;
for (set<node>::iterator it=s.begin();it!=s.end();it++,i++) {//
a[i] = it->x;
b[i] = it->y;
}
for (int i = 1;i<s.size();i++) {//判断两条线之间的关系
for (int j = i - 1;j >= 0;j--) {
int a1 = a[i];
int a2 = a[j];
int b1 = b[i];
int b2 = b[j];
if (a1 == a2) {
continue;//跳过
}
else {
node q;
q.x = 1.0 * (b2 - b1) / (a1 - a2);//相交x的坐标
q.y = 1.0 * a1 * (b2 - b1) / (a1 - a2) + b1;//相交时候y的坐标
v.insert(q);
}
}
cnt += v.size() + 1;//每次先都被分割成了两端,所以多了两个平面
}
cout << cnt << endl;
}
美好的一天总是在凌晨结束!真的累了,睡觉!!!