Small L linear

L small elementary school find themselves very artistic, I bought a piece of the drawing board, but his paintings level so that he can draw a line connecting the two points. One day he decided to have a painting on a map of n points, that he can put any connection of n points. We believe that parallel lines is very unsightly, and he wanted to know how many straight lines drawn up to make the entire drawing parallel lines do not appear.
Entry
A first line of input integer n (1 <= n <= 200)
the next two rows each row represents n integers each point coordinates x, y (-1000 <= x, y <= 1000)
Export
You can draw a line up integer number of pairwise parallel straight lines
Sample input  Copy
4
-1 1
-2 0
0 0
1 1
Sample output Copy
4
on the topic of parallel lines done before, but thought up
method of seeking parallel lines: a map memory with the current two points "slope" (only difference is stored x and y), and then negated in the presence of a (because x, y and -x, -y parallel but unequal, so we have to save twice), and then through each map may slope / 2 is equal to the number calculated in accordance with a * (a-1), then in accumulation, and finally divided by two (do not forget that we keep the two slope)
the title solution: in this problem we first squaring rural lines, but as long as we save the number of the same slope, (not inverted, it is stored As long as we have a certain slope is minus the total number of the total number once), then the same slope, then add 1 on the list
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
pair<int ,int>p;
vector<pair<int,int> >ve;
map<pair<int ,int >,int>mp;//保存斜率与斜率出现的次数
map<ll,int >mp1;
map<pair<int ,int >,int>::iterator it;
int main(){
    int n;
    int sum=0;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>p.first>>p.second;
        ve.push_back(p);//用vector保存给的数据 
    }
    int x1,a,b,x2;
    for(int i=0;i<n;i++){ 
        for(int j=i+1;j<n;j++){ 
            a=ve[i].second-ve[j].second;
            b=ve[i].first-ve[j].first;
            x1=__gcd(a,b);
            a=a/x1;
            b=b/x1;
            p.first=b;
            p.second=a;
            mp[p]++;
//            p.first=-b;
//            p.second=-a;
//            mp[p]++;
        }
    }
    int ans=0;
    int s=n*(n-1)/2;
    for(it=mp.begin();it!=mp.end();it++){
        s=s-it->second+1;
    }
    cout<<s<<endl; 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Accepting/p/11360535.html