Cod sursa(job #441083)

Utilizator DaninetDani Biro Daninet Data 12 aprilie 2010 19:11:31
Problema Trapez Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <cstdio>
#include <vector>
#include <algorithm>
struct point {
	long x,y;
};
struct slope {
	long long x1,x2,y1,y2;
	double slope;
};

bool equal(slope a, slope b) {
	if (a.x2-a.x1 == 0 && b.x2-b.x1 == 0) return true;
	if (a.x2-a.x1 == 0) return (-31220==(b.y2-b.y1)/(b.x2-b.x1));
	else if (b.x2-b.x1 == 0) return ((a.y2-a.y1)/(a.x2-a.x1)==-31220);
	else
		return ((a.y2-a.y1)/(a.x2-a.x1)==(b.y2-b.y1)/(b.x2-b.x1)); 
		
}

bool test(slope a, slope b) { 
	if (a.x2-a.x1 == 0 && b.x2-b.x1 == 0) return false;
	if (a.x2-a.x1 == 0) return (-31220<(b.y2-b.y1)/(b.x2-b.x1));
	else if (b.x2-b.x1 == 0) return ((a.y2-a.y1)/(a.x2-a.x1)<-31220);
	else
		return ((a.y2-a.y1)/(a.x2-a.x1)<(b.y2-b.y1)/(b.x2-b.x1)); 
}

int main() {
	FILE *f,*g;
	f = fopen("trapez.in","r");
	g = fopen("trapez.out","w");
	int n;
	fscanf(f,"%d\n",&n);
	point a[n+1];
	for(int i=0; i<n; i++) {
		fscanf(f,"%d %d\n",&a[i].x,&a[i].y);
	}
	std::vector<slope> b;
	slope aux;
	int rez = 0;
	for(int i=0; i<n-1; i++) {
		for(int j=i+1; j<n; j++) {
			//if (a[i].x==a[j].x) { rez++; continue; }
			aux.x1 = a[i].x;
			aux.x2 = a[j].x;
			aux.y1 = a[i].y;
			aux.y2 = a[j].y;
			//if (a[j].x-a[i].x==0) aux.slope = -32117;
			//else aux.slope = (double)((a[j].y-a[i].y)*1000)/((a[j].x-a[i].x)*1000);
			b.push_back(aux);
		}
	}
	sort(b.begin(), b.end(), test);
	
	for(int i=1; i!=(int)b.size();i++) {
		if (equal(b[i-1],b[i])) { 
			rez++; 
			i = i+2;
		}
	}
	fprintf(g,"%d",rez);

	fclose(f);
	fclose(g);
	
}