Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

jeongwon

[백준 JAVA] 10818번 - 최소, 최대 본문

오늘의 문제

[백준 JAVA] 10818번 - 최소, 최대

jeongwon_ 2022. 5. 25. 12:04

 

나의 답안: 

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

class Main{
	public static void main(String[] args) throws IOException   {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		int n=Integer.parseInt(bf.readLine());
		st=new StringTokenizer(bf.readLine()," ");
		bf.close();
		int[] num = new int[n];
		
		
		for(int i=0; i<n; i++) {			
			num[i]=Integer.parseInt(st.nextToken());			
		}		
		int min = num[0], max = num[0];
		for(int i=0; i<n-1; i++) {			
			if(min>num[i+1]) min=num[i+1];
			if(max<num[i+1]) max=num[i+1];
		}		
		System.out.println(min+" "+max);
	}	
}

 

추가 답안:

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

class Main{
	public static void main(String[] args) throws IOException   {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		int n=Integer.parseInt(bf.readLine());
		st=new StringTokenizer(bf.readLine()," ");
		bf.close();
		int[] num = new int[n];
		
		int i=0;
		while(st.hasMoreTokens()) {			//
			num[i]=Integer.parseInt(st.nextToken());			
		}		
		
		Arrays.sort(num);	//
		System.out.println(num[0]+" "+num[n-1]);
		}				
	}

(가운데 제출안은 hasMoreTokens() 메소드를 사용하지 않고, Arrays.sort() 만 적용한 경우.)