Cod sursa(job #1844307)

Utilizator Razvanel6991Razvan Lazar Razvanel6991 Data 9 ianuarie 2017 21:36:13
Problema Trapez Scor 0
Compilator c Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <stdio.h>
#include <stdlib.h>

double getSlope(int x1, int x2, int y1, int y2){
	if(x2 == x1){
		return 1.005;
	}
	double slope = (double)(y2 - y1)/(x2 - x1);
	return slope;
}

int cmpfunc (const void * a, const void * b)
{
   return ( *(double*)a - *(double*)b );
}

void sortArray(double *v, int k){
	double temp;
	for(int i = 0; i < k - 1; i++){
		for(int j = i + 1; j < k; j++){
			if(v[i] > v[j]){
				temp = v[i];
				v[i] = v[j];
				v[j] = temp;
			}
		} 
	}
}

void quick_sort(double *arr, int low, int high)
{
	int pivot,j,temp,i;
 	if(low < high)
 	{
  		pivot = low;
  		i = low;
  		j = high;
 
  		while(i<j)
  		{
   			while((arr[i]<=arr[pivot])&&(i<high))
   			{
    			i++;
   			}
 
   			while(arr[j]>arr[pivot])
   			{
    			j--;
   			}
 
   			if(i<j)
   			{ 
    			temp=arr[i];
    			arr[i]=arr[j];
    			arr[j]=temp;
   			}
  		}
 
  		temp=arr[pivot];
  		arr[pivot]=arr[j];
  		arr[j]=temp;
  		quick_sort(arr,low,j-1);
  		quick_sort(arr,j+1,high);
 	}
}


int getResult(double *v, int k){
	double val = v[0] - 1;
	int total = 0;
	for(int i = 0; i < k; i++){
		if(v[i] == val){
			total++;
		}
		else{
			val = v[i];
		}
	}
	return total; 
}
int main(){
	FILE *in, *out;
	int N, k = 0;
	int arr_x[1000], arr_y[1000];
	double slopes[500500];
	in = fopen("trapez.in", "r");
	out = fopen("trapez.out", "w");
	fscanf(in, "%d", &N);
	for( int i = 0; i < N; i++){
		fscanf(in, "%d", &arr_x[i]);
		fscanf(in, "%d", &arr_y[i]);
	}

	for(int i = 0; i < N - 1; i++){
		for(int j = i + 1; j < N; j++){
			slopes[k++] = getSlope(arr_x[i], arr_x[j], arr_y[i], arr_y[j]);
		}
	}

	qsort(slopes, k, sizeof(double), cmpfunc);
	fprintf(out, "%d\n", getResult(slopes, k));
	return 0;
}