蓝桥杯---试题 历届试题 平面切分

试题 历届试题 平面切分

资源限制
时间限制:1.0s 内存限制:256.0MB
问题描述
平面上有 条直线,其中第 条直线是 。

请计算这些直线将平面分成了几个部分。

输入格式
第一行包含一个整数 。

以下N行,每行包含两个整数 。

输出格式
一个整数代表答案。

样例输入

3
1 1
2 2
3 3

样例输出

6

评测用例规模与约定
50%样例:1<=N<=4,-10<=Ai,Bi<=10;
100%样例:1<=N<=1000,-100000<=Ai,Bi<=100000

思路:使用集合存线set<pair<int,int> > line;
然后没来一条线如果与当前线集合有m(m>0)个交点,则平面会多增加1+m个;
如果当前线与线集合中的有重合(A,B均相等),则平面数不会增加

code:

#include<set>
#include<vector>
#include<cstdio>
#include<iostream>

using namespace std;

int n;
int ans=1;
set<pair<int,int> > line;

int main()
{
    
    
    cin>>n;
    for(int i=0;i<n;i++){
    
    
        pair<int ,int>  pa;
        cin>>pa.first>>pa.second;
        int flag=1;
        set<pair<double,double> > point;
        for(set<pair<int,int> >::iterator it=line.begin();it!=line.end();it++){
    
    
            if((*it).first!=pa.first){
    
    //
                pair <double,double>  pp;
                pp.first=(pa.second-(*it).second)*1.0/(pa.first-(*it).first);
                pp.second=pa.first*pp.first+pa.second;
                point.insert(pp);
            }else if((*it).first==pa.first&&((*it).second==pa.second)){
    
    
                flag=0;
            }
        }
        line.insert(pa);
        if(flag)ans+=1+point.size();
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/timelessx_x/article/details/115431429
今日推荐