Cod sursa(job #3271974)

Utilizator domdiridomdidomDominik domdiridomdidom Data 27 ianuarie 2025 22:58:10
Problema Cele mai apropiate puncte din plan Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.06 kb
#include <climits>
#include <cmath>
#include <fstream>
#include <iomanip>
#include <iostream>

struct koord{
	long long x, y;
	koord(int a, int b){
		x = a;
		y = b;
	}
	koord(){
		x = 0;
		y = 0;
	}
	double tavolsag ( koord masik ) {
		unsigned long long tav = ( ( masik.x - x ) * ( masik.x - x ) ) + ( ( masik.y - y ) * ( masik.y - y ) );
		double d = sqrt ( tav );
		return d;
	}
};
std::istream & operator>>(std::istream & bem, koord & bemenet){
	bem >> bemenet.x >> bemenet.y;
	return bem;
}
std::ostream & operator<<(std::ostream & kim, koord & kimenet){
	kim << "( " <<kimenet.x << ", " << kimenet.y << " )";
	return kim;
}

void merge(koord tomb[], int bal, int kozep, int jobb){
	int *temp = new int[jobb - bal + 1];
	int indexTemp = 0, indexBal = bal, indexJobb = kozep + 1;
	while (indexBal <= kozep && indexJobb <= jobb) {
		if(tomb[indexBal].y <= tomb[indexJobb].y)
			temp[indexTemp++] = tomb[indexBal++].y;
		else
		 temp[indexTemp++] = tomb[indexJobb++].y;
	}
	while (indexBal <= kozep)
		temp[indexTemp++] = tomb[indexBal++].y;
	while (indexJobb <= jobb)
		temp[indexTemp++] = tomb[indexJobb++].y;
	for (int i = 0; i < indexTemp; i++)
		tomb[bal + i].y = temp[i];
	delete [] temp;
	return;
}

void sort(koord tomb[], int bal, int jobb){
	if(bal >= jobb)
		return;
	int kozep = (bal + jobb) / 2;
	sort(tomb, bal, kozep);
	sort(tomb, kozep + 1, jobb);
	merge(tomb, bal, kozep, jobb);
}

int main(){
	std::ifstream bem("cmap.in");
	int n;
	double minTav = INT_MAX;
	int DEBUG_X, DEBUG_Y;
	bem >> n;
	koord * tomb = new koord[n];
	for(int i = 0; i < n; i++)
		bem >> tomb[i];
	for(int i = 0; i < n; i++){
		for(int j = i; j < n; j++){
			if(i != j){
				double tav = tomb[i].tavolsag(tomb[j]);
				if(tav < minTav){
					minTav = tav;
					DEBUG_X = i;
					DEBUG_Y = j;
				}
			}
		}
	}
	//sort(tomb, 0, n - 1);

	/*for(int i = 0; i < n; i++)
		std::cout << tomb[i] << '\n';*/
	std::ofstream kim("cmap.out");
	//kim << tomb[DEBUG_X] << ", " << tomb[DEBUG_Y] << " : " << minTav << '\n';
	kim << std::fixed << std::setprecision(6) << minTav << '\n';

	bem.close();
	kim.close();
	delete [] tomb;
}