首页 > 代码库 > OJ刷题之《可变参数--求n维空间点之间的距离》

OJ刷题之《可变参数--求n维空间点之间的距离》

题目描述

利用可变参数求nN<5)维空间两点之间的距离。n维空间两点X(x1,,,,xn),Y(y1,...,yn)之间的距离定义为:

                        

部分代码已给定如下,只需要提交缺失的代码。

#include <stdarg.h>
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
double distance(int dime,...); //dime
表示维数,后面依次是两个点每一维的坐标 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;
}

输入

一维空间两个点的坐标 x1,y1
二维空间两个点的坐标x1,y1,x2,y2
三维空间两个点的坐标x1,y1,x2,y2,x3,y3
四维空间两个点的坐标x1,y1,x2,y2,x3,y3,x4,y4

输出

一维空间两个点的距离
二维空间两个点的距离
三维空间两个点的距离
四维空间两个点的距离

样例输入

1 2

1 2 1 2

1 2 1 2 1 2

1 2 1 2 1 2 1 2

样例输出

1.00

1.41

1.73

2.00


代码如下:

#include <stdarg.h>
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
int main()
{
double distance(int dime,double x1,double y1);
double distance(int dime,double x1,double y1,double x2,double y2);
double distance(int dime,double x1,double y1,double x2,double y2,double x3,double y3);
double 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);
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;
}

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;
}

运行结果:

技术分享



现在在纠结怎么提交。由于题目给的代码要求不清晰,要求只提交缺失的代码,但在主函数那段并没有给出明确的函数定义,只好写了四个函数。然后需要提交两段,不知道行不行。。。同时本来想用数组分别记录x1,y1,x2,y2等等,然后再用一个函数去计算,但由于数值不算多,就采取直接计算了。。。感觉还能优化很多。

OJ刷题之《可变参数--求n维空间点之间的距离》