Cod sursa(job #2888420)

Utilizator mihaioaremicamihaita the midget mihaioaremica Data 11 aprilie 2022 01:26:08
Problema Cele mai apropiate puncte din plan Scor 0
Compilator c-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#include <stdio.h>
#include <stdlib.h>
#include "math.h"

typedef struct {
    float x, y;
} Punct;

int N;

Punct *puncte;

int cmp(const void *A, const void *B)
{
    return ((Punct*)A)->x - ((Punct*)B)->x;
}

float distanta(Punct A, Punct B)
{
    return sqrt((B.x - A.x) * (B.x - A.x) + (B.y - A.y) * (B.y - A.y));
}

float min(float a, float b)
{
    if (a < b)
        return a;

    return b;
}

void citeste()
{
    FILE *fisier = fopen("cmap.in", "r");

    fscanf(fisier, "%d", &N);

    puncte = (Punct*) malloc(N * sizeof(Punct));

    for (int i = 0; i < N; i ++)
        fscanf(fisier, "%f %f", &puncte[i].x, &puncte[i].y);

    fclose(fisier);
}

float rezolva(int s, int d)
{
    int m = (s + d) / 2;
    if (d - s == 1)
        return distanta(puncte[s], puncte[d]);
    else
        return min(rezolva(s, m), rezolva(m, d));
}

int main()
{
    citeste();
    qsort(puncte, N, sizeof(Punct), cmp);
    FILE *fisier = fopen("cmap.out", "w");
    fprintf(fisier, "%.2f", rezolva(0, N - 1));
    return 0;
}