Cod sursa(job #2120306)

Utilizator adiaioanaAdia R. adiaioana Data 2 februarie 2018 12:00:40
Problema Trapez Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <algorithm>
#define NM 1005
#define maxim 2000000005
using namespace std;
ifstream fin("trapez.in");
ofstream fout("trapez.out");
int n,X,Y,nr,nrt,k;
struct chestie{
int x,y;
}v[NM*NM],a[NM];
bool comp(chestie a,chestie b);
void ired(int &a,int &b);
int main()
{
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>a[i].x>>a[i].y;
    }
    for(int i=1;i<n;i++)
        for(int j=i+1;j<=n;j++)
        {
            X=a[j].x-a[i].x;
            Y=a[j].y-a[i].y;
            ired(X,Y);
            v[++k].x=X;
            v[k].y=Y;
        }
    sort(v+1,v+k+1,comp);
    nr=1;
    for(int i=2;i<=k;i++)
    {
        if(v[i].x==v[i-1].x&&v[i].y==v[i-1].y)
            nr++;
        else if(nr>0)
        {
            nrt+=(nr*(nr-1)/2);
            nr=1;
        }
    }
    fout<<nrt<<'\n';
    return 0;
}
bool comp(chestie a,chestie b)
{
    if(a.x<b.x)
        return 1;
    if(a.x>b.x)
        return 0;
    if(a.y<b.y)
        return 1;
    return 0;
}
void ired(int &a,int &b)
{
    if(b==0)
    {
        a=1;
        return ;
    }
    if(a==0)
    {
        b=1;
        return ;
    }
    int c=a,d=b,r=c%d;
    while(r)
    {
        c=d;
        d=r;
        r=c%d;
    }
    a=a/d;
    b=b/d;
}