算法竞赛入门经典: 第四章 函数与递归 4.1求两点之间距离

/*
求两点之间距离
*/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/*
关键:
1 float hypot(float x,float y):计算指教三角形斜边长度
2 能用float尽量别用double,它会产生莫名其妙的小数
*/

typedef struct Point{
	Point(int x,int y):_x(x),_y(y){}
	int _x;
	int _y;
}Point;

float distance(Point p1,Point p2)
{
	return hypot(p1._x - p2._x,p1._y - p2._y);
}

double distance(double x1,double y1,double x2,double y2)
{
	double dx = x1 - x2;
	double dy = y1 - y2;
	return hypot(dx,dy);
}

int main(int argc,char* argv[])
{
	//double x1,y1,x2,y2;
	float  x1,y1,x2,y2;;
	scanf("%f %f %f %f",&x1,&y1,&x2,&y2);
	Point p1(x1,y1);
	Point p2(x2,y2);
	printf("%.2lf\n",distance(x1,y1,x2,y2));
	printf("%.2lf\n",distance(p1,p2));
	system("pause");
	return 0;
}

你可能感兴趣的:(算法竞赛入门)