Dalyoung 2021. 2. 3. 23:11
반응형

www.acmicpc.net/problem/2166

 

2166번: 다각형의 면적

첫째 줄에 N이 주어진다. 다음 N개의 줄에는 다각형을 이루는 순서대로 N개의 점의 x, y좌표가 주어진다. 좌표값은 절댓값이 100,000을 넘지 않는 정수이다.

www.acmicpc.net

 

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class Main {
	public static void main(String[] args) throws IOException {
		Main m = new Main();
		m.doit();
				
	}
	
	
	
	int N;
	double x[] = new double[10001];
	double y[] = new double[10001];
	public void doit() throws IOException{
	//	System.setIn(new FileInputStream("input.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		N = Integer.parseInt(br.readLine());
		String [] input;
		for(int i = 0; i < N; i++){
			input = br.readLine().split(" ");
			x[i] = Double.parseDouble(input[0]);
			y[i] = Double.parseDouble(input[1]);
		}
		double ret = 0;
		for(int i = 0; i < N-1; i++){
			ret += x[i] * y[i+1];
			ret -= x[i+1] * y[i];
		}
		ret += x[N-1] * y[0];
		ret -= x[0] * y[N-1];
		ret =  Math.round((Math.abs(ret) / 2.0)*100.0)/100.0;
		System.out.println(String.format("%.1f", ret));
		br.close();
	}
	
	public void write() throws IOException{
		PrintWriter pw = new PrintWriter("output.txt");
		pw.println("1234");
		pw.close();
	}
}
반응형