C/C++ Programming Learning-Week 10 ⑧ Calculate the distance between two points

Topic link

Title description

Input the coordinates of two points (X1, Y1), (X2, Y2), calculate and output the distance between the two points.

There
are multiple groups of input data, each group occupies a line, composed of 4 real numbers, which represent x1, y1, x2, y2, and the data are separated by spaces.

Output
For each group of input data, output one line, and the result retains two decimal places.

Sample Input

0 0 0 1
0 1 1 0

Sample Output

1.00
1.41

Ideas

This question was also discussed in the third week, that is, input the coordinates of two points, calculate and output the distance between the two points. Need to use the function sqrt() to find the square root, in the header file math.h.

C++ code:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
	double x1, x2, y1, y2;
	while(cin >> x1 >> y1 >> x2 >> y2)
	{
    
    
		double ans = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
		printf("%.2lf\n", ans);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113099762