https://www.acmicpc.net/problem/2166
2166번: 다각형의 면적
첫째 줄에 N이 주어진다. 다음 N개의 줄에는 다각형을 이루는 순서대로 N개의 점의 x, y좌표가 주어진다. 좌표값은 절댓값이 100,000을 넘지 않는 정수이다.
www.acmicpc.net
풀이
더보기
ccw 로 삼각형 구해서 다 더하면 된다.
코드
package test2166;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
static int N;
static long x[];
static long y[];
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
MyScanner sc = new MyScanner(System.in);
N = sc.nextInt();
x = new long[N + 1];
y = new long[N + 1];
for (int i = 0; i < N; i++) {
x[i] = sc.nextLong();
y[i] = sc.nextLong();
}
x[N] = x[0];
y[N] = y[0];
System.out.println(String.format("%.1f",ccw(x, y)));
sc.close();
}
static double ccw(long[] x, long[] y) {
long resultX = 0;
long resultY = 0;
for (int i = 0; i < N; i++) {
resultX += x[i] * y[i + 1];
resultY += x[i + 1] * y[i];
}
return (double)(Math.abs(resultX - resultY)) / 2;
}
static class MyScanner {
final BufferedReader reader;
final BufferedWriter writer;
static StringTokenizer tokenizer = null;
MyScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
writer = new BufferedWriter(new OutputStreamWriter(System.out));
}
String nextToken() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
void write(String sb) throws IOException {
writer.write(sb);
}
void close() throws IOException {
reader.close();
writer.close();
}
}
}
'Algorithms > 2023 Pro 시험 준비' 카테고리의 다른 글
2662 기업 투자 - 백준 (0) | 2023.08.22 |
---|---|
3691 컴퓨터조립 - 백준 (0) | 2023.08.21 |
11758 ccw - 백준 (0) | 2023.08.16 |
2957 이진 탐색 트리 - 백준 (0) | 2023.08.08 |
2613 숫자구슬 - 백준 (0) | 2023.08.08 |