92 lines
1.6 KiB
C++
92 lines
1.6 KiB
C++
|
#include <iostream>
|
||
|
#include <cstdlib>
|
||
|
#include <ctime>
|
||
|
#include <vector>
|
||
|
#include <cmath>
|
||
|
#include <iomanip>
|
||
|
using namespace std;
|
||
|
|
||
|
class Planet {
|
||
|
public:
|
||
|
int x,y,z;
|
||
|
Planet(int x, int y, int z){
|
||
|
this->x = x;
|
||
|
this->y = y;
|
||
|
this->z = z;
|
||
|
}
|
||
|
Planet(){
|
||
|
x = rand()%2000 - 1000;
|
||
|
y = rand()%2000 - 1000;
|
||
|
z = rand()%2000 - 1000;
|
||
|
}
|
||
|
|
||
|
double distanceTo(Planet p){
|
||
|
return sqrt(pow(p.x - x, 2) + pow(p.y - y, 2) + pow(p.z - z, 2));
|
||
|
}
|
||
|
|
||
|
void print(){
|
||
|
cout<<setw(6)<<x<<setw(6)<<y<<setw(6)<<z;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
int main(){
|
||
|
ios::sync_with_stdio(false);
|
||
|
|
||
|
cout<<"Planets to generate: ";
|
||
|
int n;
|
||
|
cin>>n;
|
||
|
|
||
|
cout<<"Planets:\n";
|
||
|
vector<Planet> v;
|
||
|
for(int i=0; i<n; i++){
|
||
|
v.push_back(Planet());
|
||
|
cout<<"Planet "<<setw(3)<<i<<": ";
|
||
|
v[i].print();
|
||
|
cout<<"\n";
|
||
|
}
|
||
|
cout<<"\n";
|
||
|
|
||
|
int closeA = 0;
|
||
|
int closeB = 1;
|
||
|
double closeDist = v[0].distanceTo(v[1]);
|
||
|
int farA = 0;
|
||
|
int farB = 1;
|
||
|
double farDist = v[0].distanceTo(v[1]);
|
||
|
|
||
|
for(int i=0; i<v.size(); i++){
|
||
|
for(int j=i+1; j<v.size(); j++){
|
||
|
double dist = v[i].distanceTo(v[j]);
|
||
|
|
||
|
if(dist < closeDist){
|
||
|
closeDist = dist;
|
||
|
closeA = i;
|
||
|
closeB = j;
|
||
|
}
|
||
|
|
||
|
if(dist > farDist){
|
||
|
farDist = dist;
|
||
|
farA = i;
|
||
|
farB = j;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
cout<<"The planets closest to each other:\n";
|
||
|
cout<<"Planet "<<closeA<<"(";
|
||
|
v[closeA].print();
|
||
|
cout<<") and Planet "<<closeB<<"(";
|
||
|
v[closeB].print();
|
||
|
cout<<")\n";
|
||
|
cout<<"Distance: "<<closeDist<<"\n\n";
|
||
|
|
||
|
cout<<"The planets farthest to each other:\n";
|
||
|
cout<<"Planet "<<farA<<"(";
|
||
|
v[farA].print();
|
||
|
cout<<") and Planet "<<farB<<"(";
|
||
|
v[farB].print();
|
||
|
cout<<")\n";
|
||
|
cout<<"Distance: "<<farDist<<"\n\n";
|
||
|
|
||
|
return 0;
|
||
|
}
|