Pagini recente » Cod sursa (job #2842199) | Calibrare limite de timp | Cod sursa (job #2196373) | Cod sursa (job #1787003) | Cod sursa (job #612639)
Cod sursa(job #612639)
#include <fstream>
#include <stdlib.h>
#include <math.h>
using namespace std;
int compare (const void *a, const void *b)
{
double *x = *(double**)a;
double *y = *(double**)b;
//if (x[1] < 0){ x[0] *= -1; x[1] *= -1;}
//if (y[1] < 0){ y[0] *= -1; y[1] *= -1;}
//return (x[0] * y[1]) - (x[1] * y[0]);
return (x[0] - y[0] < 0);
}
int main () {
ifstream in ("trapez.in");
ofstream out ("trapez.out");
int n, k, i, j, l, count, x = 0;
double crd [1000][2], p, q;
double **panta = (double**)calloc (1000000, sizeof(double*)), oy[10000];
in >> n;
for (i = 0; i < n; i++)
in >> crd[i][0] >> crd[i][1];
k = 0, l = 0;
for (i = 0; i < n - 1; i++)
for (j = i + 1; j < n; j++)
{
if (crd[j][0] == crd[i][0]) x++;
else
{
panta[k] = (double*) calloc (2, sizeof (double));
//panta[k][0] = crd[j][1] - crd[i][1];
//panta[k][1] = crd[j][0] - crd[i][0];
panta[k][0] = (crd[j][1] - crd[i][1])/(crd[j][0] - crd[i][0]);
k++;
}
}
qsort (panta, k, sizeof(panta[0]), compare);
count = 0;
/*for (i = 0; i < k; i++)
out << panta[i][0] / panta[i][1] <<" "<< panta[i][0]<<" "<< panta[i][1] << endl;*/
int nr = 0;
for (i = 0; i < k - 1; i++)
{
if (/*panta[i][0] * panta[i+1][1] == panta[i][1] * panta[i+1][0]*/panta[i][0] == panta[i + 1][0])
nr ++;
else
count += nr*(nr + 1)/2, nr = 0;
}
count += nr * (nr + 1) /2 + x*(x-1)/2;
out << count;
return 0;
}