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

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

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

``````
#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);
}
``````