Cod sursa(job #472161)

Utilizator marius.bucurBucur Marius - Ovidiu marius.bucur Data 23 iulie 2010 11:15:42
Problema Trapez Scor 60
Compilator c Status done
Runda Arhiva de probleme Marime 1.46 kb
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<float.h>

struct point{
	int x;
	int y;
};

int comp(const void* a, const void* b)
{
	double* d1 = (double*)a;
	double* d2 = (double*)b;
	if(*d1 < *d2)
	{
		return -1;
	}
	if(*d1 == *d2)
		return 0;
	return 1;
}

int trapez(struct point* n, int N)
{
	double* drepte = (double*)calloc(N * N, sizeof(double));
	int i = 0;
	int j = 0;
	for(; i < N*N; i++)
		drepte[i] = -DBL_MAX;
	for(i = 0; i < N; i++)
	{
		for(j = i + 1; j < N; j++)
		{
			if(n[i].y == n[j].y)
			{
				drepte[i*N + j] = DBL_MAX;
				continue;
			}
			drepte[i*N + j] = ((double)n[i].x - (double)n[j].x)/((double)n[i].y - (double)n[j].y);
		}
	}
	qsort(drepte, N*N, sizeof(double), comp);
	for(i = 0; i < N*N; i++)
		if(drepte[i] != -DBL_MAX && drepte[i] != DBL_MAX)
			printf("%f\n", drepte[i]);
		else if(drepte[i] == DBL_MAX)
			printf("dbl_max\n");
		else
			printf("dbl_min\n");
	int total = 0;
	i = 0;
	while(i + 1 < N*N)
	{
		if(drepte[i] == -DBL_MAX)
		{
			i++;
			continue;
		}
		if(drepte[i] == drepte[i+1])
		while(i + 1 < N*N && drepte[i] == drepte[i+1])
		{
			i++;
			total++;
		}
		else
			i++;
	}
	return total;
}

int main()
{
	int N = 0;
	struct point* n;
	FILE* f = fopen("trapez.in", "r");
	fscanf(f, "%d", &N);
	n = (struct point*)malloc(N * sizeof(struct point));
	int i = 0;
	while(i < N)
	{
		fscanf(f, "%d %d", &n[i].x, &n[i].y);
		i++;
	}
	fclose(f);
	f = fopen("trapez.out", "w");
	fprintf(f, "%d", trapez(n, N));
	fclose(f);
	return 0;
}