๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ ์ด์•ผ๊ธฐ/JAVA

Java - ์ตœ๋Œ“๊ฐ’ ์ตœ์†Ÿ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ Math.max() / Math.min()

by 0_0๐Ÿ–ค 2024. 1. 16.
๋ฐ˜์‘ํ˜•

 
 
 
 
๋ฌด์ž‘์œ„ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ 
์ž…๋ ฅ๋ฐ›์€ ์ˆ˜ ์ค‘ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ผ์ด ์ƒ๊ฒผ๋‹ค.
 
์ •๋ณด์ฒ˜๋ฆฌ๊ธฐ์‚ฌ c์–ธ์–ด๋ฅผ ๊ณต๋ถ€ํ•  ๋•Œ ์ตœ๋Œ€ ์ตœ์†Œ๊ฐ’ ๊ตฌํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋ดค์—ˆ๋‹ค.
๊ทธ ๊ฒฝํ—˜์„ ์‚ด๋ ค ์šฐ์„  ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ”๊พธ๊ณ  ๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•ด
๊ธฐ์–ต์„ ๋”๋“ฌ์–ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.
 

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException{
		
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        // ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜์˜ ์ด ๊ฐฏ์ˆ˜
        int count = Integer.parseInt(br.readLine());

        // ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋“ค์˜ ๋ฐฐ์—ด ์ƒ์„ฑ
        StringTokenizer st = new StringTokenizer(br.readLine());       
        int[] arr = new int[count];
        for(int i = 0; i < count; i++) arr[i] = Integer.parseInt(st.nextToken());

        int max = arr[0];
        int min = arr[0];

        for(int i = 0; i < count; i++){
            if(arr[i] > max){
                max = arr[i];
            }
            if(arr[i] < min){
                min = arr[i];
            }
        }
        br.close();
        System.out.print(min+" "+max);
    }
}

 
 
์ข€ ๋” ํ™•์ธํ•ด๋ณด๋‹ˆ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•  ๋•Œ
์ž๋ฐ”์—์„œ๋Š” Math.max(), Math.min() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
 

 


 

Math.max()

๋‘ ์ธ์ž๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ํฐ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
 

System.out.println(Math.max(10, 5)); // 10 ์ถœ๋ ฅ๋œ๋‹ค
System.out.println(Math.max(2.5, 1.2)); // 2.5 ์ถœ๋ ฅ๋œ๋‹ค

 

 
 

Math.min()

๋‘ ์ธ์ž๊ฐ’์„ ๋น„๊ตํ•˜๊ณ  ์ž‘์€ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค
 

System.out.println(Math.min(10, 5)); // 5 ์ถœ๋ ฅ๋œ๋‹ค
System.out.println(Math.min(2.5, 1.2)); // 1.2 ์ถœ๋ ฅ๋œ๋‹ค

 
 
 


 
 
Math.max() ์™€ Math.min() ์ ์šฉํ•œ
์ž…๋ ฅํ•œ ๊ฐ’ ์ค‘ ์ตœ๋Œ“๊ฐ’ ์ตœ์†Ÿ๊ฐ’ ๋ฐ˜ํ™˜ํ•˜๊ธฐ
 

import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        // ์ž…๋ ฅ๋ฐ›์„ ์ˆซ์ž์˜ ์ด ๊ฐฏ์ˆ˜
        int count = Integer.parseInt(br.readLine());
        
        // ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋“ค์˜ ๋ฐฐ์—ด ์ƒ์„ฑ
        StringTokenizer st = new StringTokenizer(br.readLine());

        int[] arr = new int[count];

        for(int i = 0; i < count; i++) arr[i] = Integer.parseInt(st.nextToken());

        int max = arr[0];
        int min = arr[0];

        for(int i = 0; i < count; i++){
            max = Math.max(max, arr[i]);
            min = Math.min(min, arr[i]);
        }

        br.close();

        System.out.print(min+" "+max);
    }
}

 
 

๋‘ ์ฝ”๋“œ ํ…Œ์ŠคํŠธ ์‹œ
์กฐ๊ฑด๋ฌธ์„ ์ด์šฉํ–ˆ์„ ๋•Œ๋ณด๋‹ค Math.max() ์™€ Math.min() ์˜ ์‚ฌ์šฉ์ด ์„ฑ๋Šฅ๋ถ€๋ถ„์—์„œ ๋‹ค์†Œ ๋–จ์–ด์ง€๋Š” ๊ฒƒ์„ ํ™•์ธ.
(์กฐ๊ฑด๋ฌธ ์‚ฌ์šฉ์‹œ 504ms , max() min() ์‚ฌ์šฉ์‹œ 508ms)
 
๊ทธ๋ž˜๋„ ๊ฐ€๋…์„ฑ๋ฉด์—์„œ๋Š” max, min์ด๋ผ๊ณ  ์žˆ์œผ๋ฉด ์ตœ๋Œ€ ์ตœ์†Œ๋ฅผ ๊ตฌํ•˜๋Š” ๊ฒƒ์ž„์„ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ
์ƒํ™ฉ์— ๋”ฐ๋ผ ์ ์ ˆํžˆ ์„ ํƒํ•ด ์‚ฌ์šฉํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.
 
 
 

๋ฐ˜์‘ํ˜•