본문 바로가기
알고리즘/백준

[백준] 9063 - 대지 (JAVA/ 자바)

by pandastic 2024. 4. 14.
반응형

9063번 문제(1)
9063번 문제(2)

 

 

1. Scanner를 이용한 방법

import java.util.Scanner;

public class Main{
public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int xmax = -10000;
    int xmin = 10000;
    int ymax = -10000;
    int ymin = 10000;

    for(int i=0; i<N; i++){
        int x = sc.nextInt();
        int y = sc.nextInt();

        xmax = Math.max(xmax, x);
        xmin = Math.min(xmin, x);
        ymax = Math.max(ymax, y);
        ymin = Math.min(ymin, y);
    }
    System.out.println((xmax-xmin)*(ymax-ymin));
    sc.close();
    }
}

 

2. BufferedReader를 이용한 방법

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 xmax = -10000;
    int xmin = 10000;
    int ymax = -10000;
    int ymin = 10000;

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

        xmax = Math.max(xmax, x);
        xmin = Math.min(xmin, x);
        ymax = Math.max(ymax, y);
        ymin = Math.min(ymin, y);
    }
    System.out.println((xmax-xmin)*(ymax-ymin));
    br.close();
    }
}
반응형