Cod sursa(job #407311)

Utilizator nomemoryAndrei Ciobanu nomemory Data 2 martie 2010 11:13:04
Problema Trapez Scor 40
Compilator c Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct point_s {
    int x;
    int y;
} point;

int main(){
    int np, npnt, i, j, k, xdif;
    point *pointarray;
    double *pnt;

    FILE *in,*out;

    in = fopen("trapez.in","r");
    fscanf(in, "%d", &np);

    pointarray = calloc(np, sizeof(*pointarray));
    for(i = 0; i < np; i++){
       fscanf(in, "%d", &pointarray[i].x);
       fscanf(in, "%d", &pointarray[i].y);
    }
    fclose(in);

    npnt = (np-1) * np / 2;
    pnt = calloc(npnt , sizeof(*pnt));
    for(k =0 , i = 0 ; i < np ; i++){
        for(j = i+1; j < np ; j++ , k++){
            xdif = pointarray[i].x - pointarray[j].x;
            if( xdif == 0 ) {
                pnt[k] = INT_MAX;
            }
            else {
                pnt[k] = (double) ( pointarray[i].y - pointarray[j].y ) / xdif;
            }
        }
    }

    free(pointarray);

    for(k = 0, i = 0; i < npnt ; i++){
        for(j = i+1; j < npnt; j++){
            if(pnt[i] == pnt[j]){
                k++;
            }
        }
    }

    free(pnt);

    out = fopen("trapez.out","w");
    fprintf(out, "%d", k);
    fclose(out);

    return (0);
}