Cod sursa(job #407422)

Utilizator nomemoryAndrei Ciobanu nomemory Data 2 martie 2010 12:30:34
Problema Trapez Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

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

int cmp(const void *p1, const void *p2);

int cmp(const void *p1, const void *p2){
    double res_d;
    int res;

    res_d = *(double*)p1 - *(double*)p2;

    if(res_d > 0){
        res = 1;
    }
    else if ( res_d < 0){
        res = -1;
    }
    else {
        res = 0;
    }

    return (res);
}

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

    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);

    qsort(pnt, npnt, sizeof(*pnt), cmp);
    for(i = 0; i < npnt; i++){
        printf("%15.3f \n", pnt[i]);
    }

    for(t = 0, i = 0; i < npnt ;){
        for(k = 0, j = i + 1; j < npnt; j++){
            if(pnt[i] != pnt[j]){
                break;
            }
            else{
                k++;
            }
        }
        t += (k + 1) * k / 2;
        i += k + 1;
        printf("i = %d t = %d \n", i, t);
    }

    free(pnt);

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

    return (0);
}