Ătant donnĂ© les coordonnĂ©es de trois points A
, B
, C
dans un plan orthonormé (O, i, j), calculez le périmÚtre du triangle formé par ces trois points.
Trois lignes contenant chacune deux entiers, séparés par un espace :
xA yA
xB yB
xC yC
Contraintes :
-1000 †xi, yi †1000
pour chaque i â {A, B, C}
Un nombre réel, arrondi à 2 chiffres aprÚs la virgule, représentant le périmÚtre.
Quand deux points coïncident, le triangle devient dégénéré (ligne droite).
Dans ce cas, il faut Ă©viter de compter deux fois la mĂȘme distance :
â Il faut donc :
1 2
3 3
-2 2
10.34
1 1
1 1
4 5
5.00
Explication :
A = B â Le triangle nâest quâun segment entre A et C â Distance = 5.00
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
float distance(int x1, int y1, int x2, int y2) {
if (x1 == x2 && y1 == y2)
return 0.0;
return sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
}
int main() {
int xA, yA, xB, yB, xC, yC;
scanf("%d %d", &xA, &yA);
scanf("%d %d", &xB, &yB);
scanf("%d %d", &xC, &yC);
float peri;
if (xA == xB && yA == yB)
peri = distance(xA, yA, xC, yC);
else if (xB == xC && yB == yC)
peri = distance(xA, yA, xB, yB);
else if (xA == xC && yA == yC)
peri = distance(xA, yA, xB, yB);
else
peri = distance(xA, yA, xB, yB) + distance(xB, yB, xC, yC) + distance(xC, yC, xA, yA);
printf("%.2f\n", peri);
return 0;
}
import math
def distance(p1, p2):
if p1 == p2:
return 0.0
return math.sqrt((p1[0] - p2[0])**2 + (p1[1] - p2[1])**2)
def perimeter(a, b, c):
if a == b or b == c:
return distance(a, c)
if a == c:
return distance(a, b)
peri = 0.0
peri += distance(a, b) + distance(b, c) + distance(c, a)
return peri
if __name__ == "__main__":
a = tuple(map(int, input().split()))
b = tuple(map(int, input().split()))
c = tuple(map(int, input().split()))
result = perimeter(a, b, c)
print(f"{result:.2f}")
đ ⏠Retour Ă la liste des challenges