BOJ_2166_다각형의면적_java

2023. 1. 11. 22:51Algorithm/BOJ

728x90

https://www.acmicpc.net/problem/2166

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 백준 2166번 다각형의면적
 골드 5
 신발끈 공식 (https://ko.wikipedia.org/wiki/%EC%8B%A0%EB%B0%9C%EB%81%88_%EA%B3%B5%EC%8B%9D)
 */
public class BOJ_2166_다각형의면적 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;

        int N = Integer.parseInt(br.readLine());
        double x[] = new double[N+1];
        double y[] = new double[N+1];
        double sum1 = 0;
        double sum2 = 0;

        for (int i=0; i<N; i++){
            st = new StringTokenizer(br.readLine());
            x[i] = Integer.parseInt(st.nextToken());
            y[i] = Integer.parseInt(st.nextToken());
        }
        x[N] = x[0];
        y[N] = y[0];

        for (int i = 0; i<N; i++){
            sum1 += x[i] * y[i+1];
            sum2 += x[i+1] * y[i];
        }
        System.out.println(String.format("%.1f", Math.abs(sum1 - sum2) / 2.0));
    }
}

'Algorithm > BOJ' 카테고리의 다른 글

BOJ_1806_부분합_java  (0) 2023.01.12
BOJ_2467_용액_java  (0) 2023.01.12
BOJ_12852_1로 만들기 2_java  (0) 2023.01.10
BOJ_14938_서강그라운드_java  (0) 2023.01.09
BOJ_13172_Σ_java  (0) 2023.01.06