算法与数据结构_计算两点之间的距离(欧氏距离)

距离公式:
在这里插入图片描述
在C++中实现时,用到了math.h文件中的pow(double x,double y)函数,这个函数的功能是求x的y次幂,例子如下:

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
	double x = 2, y = 8, z;
	z = pow(x, y);// 计算x的y次方
	cout << z << endl;
	system("pause");
	return 1;
}

结果:2的8次方 256
在这里插入图片描述

#include <iostream>
#include <math.h>
using namespace std;
float twoPointDistance(float x1, float y1, float x2, float y2);
int main()
{
	float x1=829, y1=339, x2=829, y2=341;
	cout << twoPointDistance(829, 339, 829, 342) << endl;
	system("pause");
	return 1;
}
float twoPointDistance(float x1 ,float y1 , float x2 , float y2 )
{
	return  pow(pow(x1 - x2, 2) + pow(y1 - y2, 2), 0.5);
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值