Здравствуйте, не могу понять как написать код для этого задания на C++, есть догадки?
В одномерном массиве заданы координаты N - точек в про-странстве путем перечисления вначале всех x-координат, затем y-координат, и потом z-координат. Определить координаты центра и радиус сферы, вмещающей все указанные в массиве точки.
C/C++
Программа по C++. Определить координаты радиусы и центра сферы.
#include <algorithm>
#include <iostream>
#include <random>
#include <iomanip>
#include <set>
using namespace std;
struct Point {
double x;
double y;
double z;
Point() : x(0), y(0), z(0) {}
double length(const Point& p)const {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2) + pow(z - p.z, 2));
}
friend ostream& operator<<(ostream& out, const Point& p) {
return out << "{ " << p.x << ", " << p.y << ", " << p.z << " }";
}
};
struct Less {
bool operator()(const pair<Point, Point>& a, const pair<Point, Point>& b)const {
auto la = a.first.length(a.second);
auto lb = b.first.length(b.second);
return la < lb;
}
};
int main() {
uniform_int_distribution<> uid(-50, 50);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int x) { cout << setw(4) << x; };
int m = 6;
int n = m * 3;
auto coords = new int[n];
generate(coords, coords + n, rand);
cout << "x:";
for (auto i = 0; i < n; ++i) {
if (i == m) cout << "\ny:";
if (i == (m << 1)) cout << "\nz:";
cout << setw(4) << coords[i];
}
puts("\n");
auto points = new Point[m];
for (auto i = 0; i < m; ++i) {
points[i].x = coords[i];
points[i].y = coords[i + m];
points[i].z = coords[i + (m << 1)];
}
set<pair<Point, Point>, Less> res;
for (auto i = 0; i < m; ++i) {
for (auto j = 0; j < m; ++j) {
if (j == i) continue;
auto ij = points[i].length(points[j]);
bool flag = true;
for (auto k = 0; k < m; ++k) {
if (k == j || k == i) continue;
auto ik = points[i].length(points[k]);
if (ik > ij) {
flag = !flag;
break;
}
}
if (flag) {
res.insert({ points[i], points[j] });
}
}
}
cout << "O: " << res.begin()->first << "\nM: " << res.begin()->second << '\n';
delete[] points;
delete[] coords;
system("pause > nul");
}
#include <iostream>
#include <random>
#include <iomanip>
#include <set>
using namespace std;
struct Point {
double x;
double y;
double z;
Point() : x(0), y(0), z(0) {}
double length(const Point& p)const {
return sqrt(pow(x - p.x, 2) + pow(y - p.y, 2) + pow(z - p.z, 2));
}
friend ostream& operator<<(ostream& out, const Point& p) {
return out << "{ " << p.x << ", " << p.y << ", " << p.z << " }";
}
};
struct Less {
bool operator()(const pair<Point, Point>& a, const pair<Point, Point>& b)const {
auto la = a.first.length(a.second);
auto lb = b.first.length(b.second);
return la < lb;
}
};
int main() {
uniform_int_distribution<> uid(-50, 50);
mt19937 gen{ random_device()() };
auto rand = [&] { return uid(gen); };
auto show = [](int x) { cout << setw(4) << x; };
int m = 6;
int n = m * 3;
auto coords = new int[n];
generate(coords, coords + n, rand);
cout << "x:";
for (auto i = 0; i < n; ++i) {
if (i == m) cout << "\ny:";
if (i == (m << 1)) cout << "\nz:";
cout << setw(4) << coords[i];
}
puts("\n");
auto points = new Point[m];
for (auto i = 0; i < m; ++i) {
points[i].x = coords[i];
points[i].y = coords[i + m];
points[i].z = coords[i + (m << 1)];
}
set<pair<Point, Point>, Less> res;
for (auto i = 0; i < m; ++i) {
for (auto j = 0; j < m; ++j) {
if (j == i) continue;
auto ij = points[i].length(points[j]);
bool flag = true;
for (auto k = 0; k < m; ++k) {
if (k == j || k == i) continue;
auto ik = points[i].length(points[k]);
if (ik > ij) {
flag = !flag;
break;
}
}
if (flag) {
res.insert({ points[i], points[j] });
}
}
}
cout << "O: " << res.begin()->first << "\nM: " << res.begin()->second << '\n';
delete[] points;
delete[] coords;
system("pause > nul");
}
Тут нет условий вроде "сфера должна быть минимального радиуса" и т. д., так что в качестве центра можно взять любую фиксированную точку (например, начало координат), а радиус найти просто как максимальное расстояние от центра до одной из заданных точек.
Похожие вопросы
- Написать программу на C++.Создать класс vector3d, задаваемый тройкой координат. Создать конструктор...
- Помогите написать программу на C++
- Написать программу на c++
- Помоги пожалуйста написать программу на C++
- Помогите с программой на C++
- ПОМОГИТЕ СОСТАВИТЬ ПРОГРАММУ НА C++.
- Короткая программа на C++
- Помогите написать программу на C++
- Составить программу на c++ УСПОЛЬЗУЯ МАССИВ!!!
- Помогите пожалуйста составить правильную программу на C++ 12 вариант