可变参数--求n维空间点之间的距离

可变参数--求n维空间点之间的距离

时间: 1ms        内存:128M

描述:

利用可变参数求n(N<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

提示:

参考答案(内存最优[1108]):

#include <stdio.h>
#include <math.h>
double distance1(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 distance3(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 distance2(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 distance4(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;
}
int main()
{
    int dime;
    double x1,y1,x2,y2,x3,y3,x4,y4,d;
    dime =1;
    scanf("%lf%lf",&x1,&y1);
    d = distance1(dime,x1,y1);
    printf("%.2lf\n",d);
    dime =2;
    scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
    d = distance2(dime,x1,y1,x2,y2);
    printf("%.2lf\n",d);
    dime =3;
    scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3);
    d = distance3(dime,x1,y1,x2,y2,x3,y3);
    printf("%.2lf\n",d);
    dime =4;
    scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
    d = distance4(dime,x1,y1,x2,y2,x3,y3,x4,y4);
    printf("%.2lf\n",d);
    return 0;
}

参考答案(时间最优[0]):


#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;
}
double distance(int argc, ...)
{
    // 声明一个指针, 用于持有可变参数
    va_list pArg;
    // 将 pArg 初始化为指向第一个参数
    va_start(pArg, argc);
    // 输出参数
	double tmp1[123],tmp2[123],tmp=0;
	int  i;
for(i=0;i!=argc;i++){
        // 获取 pArg 所指向的参数并输出
	tmp1[i]=va_arg(pArg, double);
	tmp2[i]=va_arg(pArg, double);
       // printf("%d, ", va_arg(pArg, int) );
    }
for(int j=0;j<=i/2+1;j++)tmp=tmp+(tmp1[j]-tmp2[j])*(tmp1[j]-tmp2[j]);//printf("%d,",tmp1[j]);
//for(j=0;j<i/2;j++)printf("%d,",tmp2[j]);
    va_end(pArg);
	//printf("%d",tmp);
	return sqrt(tmp);
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注