Write a program that calculates the distance of 2 points from the origin, as well as from each other. The distance between 2 points can be calculated using the formula:
d=√(〖(x_2-x_1)〗^2+〖(y_2-y_1)〗^2+〖(z_2-z_1)〗^2 )
第一次用,试试行不行,行的话以后会标注悬赏。
#include <iostream>
#include <math>
using namespace std;
double dist(double x1,double x2,double y1,double y1,double z1,double z2)
{
return sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) + (z2-z1)*(z2-z1));
}
int main()
{
double x1,x2,y1,y2,z1,z2;
cin >> x1 >> y1 >> z1;
cin >> x2 >> y2 >> z2;
cout << dist(x1,0,y1,0,z1,0) << endl;
cout << dist(x2,0,y2,0,z2,0) << endl;
cout << dist(x1,x2,y1,y2,z1,z2) << endl;
return 0;
}