c++ Va rog ajutatima
Răspunsuri la întrebare
#include <iostream>
#include <fstream>
#include <cmath>
struct Punct {
double x, y;
double distanta() const {
return sqrt(x * x + y * y);
}
bool operator<(const Punct& p) const {
return distanta() < p.distanta();
}
};
std::ostream& operator<<(std::ostream& out, const Punct& p) {
return out << p.x << ' ' << p.y << ' ' << p.distanta();
}
void selection_sort(Punct* lhs, const size_t n) {
Punct *mx;
for (size_t j, i = 0; i < n - 1; ++i) {
mx = lhs + i;
for (j = i + 1; j < n; ++j)
if (*mx < lhs[j])
mx = lhs + j;
std::swap(*mx, lhs[i]);
}
}
int main() {
size_t n;
Punct *v;
{
std::ifstream fin("puncte.in");
fin >> n;
v = new Punct[n];
for (size_t i = 0; i < n; ++i)
fin >> v[i].x >> v[i].y;
}
selection_sort(v, n);
std::ofstream fout("puncte.out");
for (size_t i = 0; i < n; ++i)
fout << v[i] << '\n';
delete[] v;
}