#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <iomanip>
struct point
{
int x, y, z;
};
double distance(point l, point r)
{
int x = l.x - r.x;
int y = l.y - r.y;
int z = l.z - r.z;
return std::sqrt(x*x + y*y + z*z);
}
bool orderByDist(point l, point r, point R)
{
return distance(l, R) < distance(r, R);
}
int main()
{
//Initial array
point arr[5] = {{1,1,1}, {1,2,3}, {-2, 5, 7}, {0, 5, -3}, {-1, 4, -2}};
//Reference point
point Ref = {1,1,1};
using namespace std::placeholders;
auto pred = std::bind(orderByDist, _1, _2, Ref);
std::sort(arr, arr + 5, pred);
//Output
for(point s: arr)
std::cout << std::setw(3) << s.x <<
std::setw(3) << s.y <<
std::setw(3) << s.z <<
std::setw(6) << std::setprecision(3) << distance(s, Ref) << std::endl;
}
I2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KCgpzdHJ1Y3QgcG9pbnQKewogICAgaW50IHgsIHksIHo7Cn07Cgpkb3VibGUgZGlzdGFuY2UocG9pbnQgbCwgcG9pbnQgcikKewogICAgaW50IHggPSBsLnggLSByLng7CiAgICBpbnQgeSA9IGwueSAtIHIueTsKICAgIGludCB6ID0gbC56IC0gci56OwogICAgcmV0dXJuIHN0ZDo6c3FydCh4KnggKyB5KnkgKyB6KnopOwp9Cgpib29sIG9yZGVyQnlEaXN0KHBvaW50IGwsIHBvaW50IHIsIHBvaW50IFIpCnsKICAgIHJldHVybiBkaXN0YW5jZShsLCBSKSA8IGRpc3RhbmNlKHIsIFIpOwp9CgppbnQgbWFpbigpCnsKICAgIC8vSW5pdGlhbCBhcnJheQogICAgcG9pbnQgYXJyWzVdID0ge3sxLDEsMX0sIHsxLDIsM30sIHstMiwgNSwgN30sIHswLCA1LCAtM30sIHstMSwgNCwgLTJ9fTsKICAgIC8vUmVmZXJl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