본문 바로가기
백준/5001 - 10000

[백준] 9063번 : 대지(JAVA)

by lms0806 2021. 10. 27.
728x90
반응형

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

 

9063번: 대지

입력은 표준입력(standard input)을 통해 받아들인다. 입력의 첫 줄에는 테스트 케이스의 개수 T (1 ≤ T ≤ 20)가 주어진다. 각 테스트 케이스의 첫째 줄에는 점의 개수 N (1 ≤ N ≤ 100,000) 이 주어진다.

www.acmicpc.net

풀이

풀이방식으로는 여러가지가 있습니다.

1. 1차원배열 2개를 만들어 정렬하고 (첫배열마지막 - 첫배열처음) * (둘배열마지막 - 둘배열처음)

2. max 2개, min 2개를 만들어 (max1 - min1) * (max2 - min2)

 

소스코드

1.

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

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		
		int n = Integer.parseInt(br.readLine());
		
		int max = 0, min = Integer.MAX_VALUE, max1 = 0, min1 = Integer.MAX_VALUE;
		while(n --> 0) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			int a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken());
			
			max = Math.max(max, a);
			min = Math.min(min, a);
			max1 = Math.max(max1, b);
			min1 = Math.min(min1, b);
		}
		System.out.print((max - min) * (max1 - min1));
	}
}

2. 

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

public class Main {
	public static void main(String[] args) throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
		
		int n = Integer.parseInt(br.readLine());
		
		int[] x = new int[n], y = new int[n];
		
		for(int i = 0; i < n; i++) {
			StringTokenizer st = new StringTokenizer(br.readLine());
			x[i] = Integer.parseInt(st.nextToken());
			y[i] = Integer.parseInt(st.nextToken());
		}
		
		Arrays.sort(x);
		Arrays.sort(y);
		
		System.out.print((x[n - 1] - x[0]) * (y[n - 1] - y[0]));
	}
}

 

728x90
반응형

댓글