OJ "Variable parameters--finding the distance between n-dimensional space points"

Source: Internet
Author: User

Title Description

The use of variable parameters to find N ( n<5 ) The distance between two points of the dimension space. N - dimensional space two points X (x1,,,, xn), the distance between Y (Y1,..., yn) is defined as:

Some of the code is given below, just submit the missing code.

#include <stdarg.h>
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace Std;
int main ()
{
Double distance (int dime,...); Dime represents the number of dimensions , followed by the coordinates of each dimension of two points x1,y1,x2,y2,x3,x3,...
int dime;
Double x1,y1,x2,y2,x3,y3,x4,y4,d;
Cout<<setiosflags (ios::fixed) <<setprecision (2);
Dime = 1;
cin>>x1>>y1;
D = distance (dime,x1,y1);
cout<<d<<endl;

Dime = 2;
cin>>x1>>y1>>x2>>y2;
D = distance (dime,x1,y1,x2,y2);
cout<<d<<endl;

Dime = 3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
D = distance (dime,x1,y1,x2,y2,x3,y3);
cout<<d<<endl;

Dime = 4;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
D = distance (DIME,X1,Y1,X2,Y2,X3,Y3,X4,Y4);
cout<<d<<endl;

return 0;
}

Input

coordinates of two points in one-dimensional space x1,y1
the coordinates of two-dimensional space. X1,y1,x2,y2
coordinates of two points in three-dimensional space X1,y1,x2,y2,x3,y3
coordinates of two points in four-dimensional space X1,y1,x2,y2,x3,y3,x4,y4

Output

The distance between two points in one-dimensional space
The distance between two points in two-dimensional space
The distance between two points in three-dimensional space
Distance between two points in four-dimensional space

Sample input

1 2

1 2 1 2

1 2 1 2 1 2

1 2 1 2 1 2 1 2

Sample output

1.00

1.41

1.73

2.00


The code is as follows:

#include <stdarg.h> #include <iostream> #include <math.h> #include <iomanip>using namespace std ; int main () {double distance (int dime,double x1,double y1);d ouble distance (int dime,double x1,double y1,double x2,double y 2);d ouble distance (int dime,double x1,double y1,double x2,double y2,double x3,double y3);d ouble distance (int dime,double X1,double y1,double x2,double y2,double x3,double y3,double x4,double y4); int dime;double x1,y1,x2,y2,x3,y3,x4,y4,d; Cout<<setiosflags (ios::fixed) <<setprecision (2);d ime =1;cin>>x1>>y1;d = distance (dime,x1, y1); Cout<<d<<endl;dime =2;cin>>x1>>y1>>x2>>y2;d = distance (dime,x1,y1,x2,y2); Cout<<d<<endl;dime =3;cin>>x1>>y1>>x2>>y2>>x3>>y3;d = Distance ( DIME,X1,Y1,X2,Y2,X3,Y3); Cout<<d<<endl;dime =4;cin>>x1>>y1>>x2>>y2>>x3 >>y3>>x4>>y4;d = distance (dime,x1,y1,x2,y2,x3,y3,x4,y4); cout&Lt;<d<<endl;return 0;}    Double distance (int dime,double x1,double y1) {double sum=0,m;    int i=0;    M=x1-y1;    M=m*m;    while (I<dime) {sum+=sqrt (M);    i++; } return sum;}    Double distance (int dime,double x1,double y1,double x2,double y2) {double sum=0,str[dime];    int i=0;    Str[0]=x1-y1;    Str[1]=x2-y2;        while (I<dime) {sum+= (str[i]*str[i]);    i++;    } sum=sqrt (sum); return sum;}    Double distance (int dime,double x1,double y1,double x2,double y2,double x3,double y3) {double sum=0,str[dime];    int i=0;    Str[0]=x1-y1;    Str[1]=x2-y2;    Str[2]=x3-y3;        while (I<dime) {sum+= (str[i]*str[i]);    i++;    } sum=sqrt (sum); return sum;} Double distance (int dime,double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4) {double sum=0    , Str[dime];    int i=0;    Str[0]=x1-y1;    Str[1]=x2-y2;    Str[2]=x3-y3;    Str[3]=x4-y4;   while (I<dime) {sum+= (str[i]*str[i]);     i++;    } sum=sqrt (sum); return sum;}

Operation Result:




Now in tangled how to submit. Because the code required by the topic is not clear, requires only the missing code, but in the main function that paragraph did not give a definite function definition, had to write four functions. Then need to submit two paragraphs, do not know the line ... At the same time originally want to use the array to record x1,y1,x2,y2, and so on, and then use a function to calculate, but because the value is not much, take direct calculation ... The feeling can also be optimized a lot.

OJ "Variable parameters--finding the distance between n-dimensional space points"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.