Cod sursa(job #397569)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 17 februarie 2010 10:29:10
Problema Triang Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
# include <stdio.h>
# include <stdlib.h>
# include <math.h>

# define EPS 1e-4

const long int MAXN=1500;

typedef struct PUNCT{
        float x,y;
        } PUNCT;
long int n,sol;
        
PUNCT p[MAXN+1];

int compara(const void *aa, const void *bb){
    PUNCT *a=(PUNCT*)aa;
    PUNCT *b=(PUNCT*)bb;
    if (fabs(a->x-b->x)<=EPS) 
       {
       if (fabs(a->y-b->y)<=EPS) return 0;
       if (a->y<b->y) return -1;
       return 1;
       }
    if (a->x<b->x) return -1;
    return 1;
}

void citire()
{
     FILE *f=fopen("triang.in","r");
     fscanf(f,"%ld",&n);
     long int i;
     for (i=1;i<=n;i++)
         fscanf(f,"%f%f",&p[i].x,&p[i].y);
     fclose(f);
}

void calculeaza()
{
     long int i,j;

     const float A=0.5;
     const float B=sqrt(3)/2;
     float xc,yc;
     PUNCT c;
     void *found;
     
     qsort(p+1,n,sizeof(PUNCT),compara);

     for (i=1;i<=n;i++)
         for (j=1;j<=n;j++)
             if (i!=j)
             {
             c.x=A*(p[j].x+p[i].x)-B*(p[j].y-p[i].y);
             c.y=A*(p[j].y+p[i].y)+B*(p[j].x-p[i].x);
             found = bsearch(&c,p+1,n,sizeof(PUNCT),compara);
             if (found) sol++;
             }
}

void scrie()
{
       FILE *g=fopen("triang.out","w");
       fprintf(g,"%ld\n",sol/3);
       fclose(g);
}

int main(){
    citire();
    calculeaza();
    scrie();
    return 0;
}