Algorithm
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1015
Distance Between Two Points
Adapted by Neilor Tonin, URI Brazil
Read the four values corresponding to the x and y axes of two points in the plane, p1 (x1, y1) and p2 (x2, y2) and calculate the distance between them, showing four decimal places after the comma, according to the formula:
Distance =
Input
The input file contains two lines of data. The first one contains two double values: x1 y1 and the second one also contains two double values with one digit after the decimal point: x2 y2.
Output
Calculate and print the distance value using the provided formula, with 4 digits after the decimal point.
Input Sample | Output Sample |
1.0 7.0 |
4.4721 |
-2.5 0.4 |
16.1484 |
2.5 -0.4 |
16.4575 |
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include<stdio.h>
#include<math.h>
int main()
{
double x1,y1,x2,y2,d,x;
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
x=((pow((x2-x1),2))+(pow((y2-y1),2)));
d=sqrt(x);
printf("%.4lf\n",d);
return 0;
//Here is the end of this program.
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with Java Programming
Code -
Java Programming
import java.io.IOException;
import java.util.Scanner;
import java.lang.Math;
public class Main{
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
float x1 = input.nextFloat();
float y1 = input.nextFloat();
float x2 = input.nextFloat();
float y2 = input.nextFloat();
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
System.out.printf("%.4f\n", distance);
}
}
Copy The Code &
Try With Live Editor
Input
Output
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
// var lines = input.split('\n');
var sep = [' ', '\n'];
// console.log(separators.join('|'));
var line = input.split(new RegExp(sep.join('|'), 'g'));
var x1 = parseFloat(line.shift());
var y1 = parseFloat(line.shift());
var x2 = parseFloat(line.shift());
var y2 = parseFloat(line.shift());
var distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
console.log(distance.toFixed(4));
Copy The Code &
Try With Live Editor
Input
Output
#4 Code Example with Python Programming
Code -
Python Programming
from math import sqrt, pow
x1, y1 = map(float, input().split())
x2, y2 = map(float, input().split())
distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2))
print("{:.4f}".format(distance))
Copy The Code &
Try With Live Editor
Input
Output