728x90
https://www.acmicpc.net/problem/2166
2166번: 다각형의 면적
첫째 줄에 N이 주어진다. 다음 N개의 줄에는 다각형을 이루는 순서대로 N개의 점의 x, y좌표가 주어진다. 좌표값은 절댓값이 100,000을 넘지 않는 정수이다.
www.acmicpc.net
외적을 통해 넓이를 구하면 쉽게 풀 수 있습니다.
def solution(N, data_list):
# 데이터 x 좌표 y 좌표 분리
x_list = []
y_list = []
for x, y in data_list:
x_list.append(x)
y_list.append(y)
x_list.append(x_list[0])
y_list.append(y_list[0])
# 외적 합
sum_cross = 0
# 외적
for i in range(N):
sum_cross += x_list[i]*y_list[i+1]
sum_cross -= y_list[i]*x_list[i+1]
sum_cross = abs(sum_cross)/2
print(f'{round(sum_cross, 1):.1f}')
N = int(input())
data_list = [list(map(int, input().split())) for _ in range(N)]
solution(N, data_list)
728x90
'Coding Test > BaekJoon_Python' 카테고리의 다른 글
백준 25308 <방사형 그래프> Python (0) | 2023.12.18 |
---|---|
백준 11758 <CCW> Python (1) | 2023.12.17 |
백준 4386 <별자리 만들기> Python (1) | 2023.12.15 |
백준 1197 <최소 스패닝 트리> Python (0) | 2023.12.14 |
백준 9372 <상근이의 여행> Python (0) | 2023.12.13 |