본문 바로가기

프로그래밍언어/알고리즘

백준 10871 X보다 작은 수

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

 

10871번: X보다 작은 수

첫째 줄에 N과 X가 주어진다. (1 ≤ N, X ≤ 10,000) 둘째 줄에 수열 A를 이루는 정수 N개가 주어진다. 주어지는 정수는 모두 1보다 크거나 같고, 10,000보다 작거나 같은 정수이다.

www.acmicpc.net

 

 

 

 

 

1. 배열사용



import java.util.Scanner;

public class Main{
       //BufferedReader를 사용하기위해선 예외처리가 필수
   public static void main(String[] args)  {
	   
	   Scanner sc = new Scanner(System.in);

	   int n  = sc.nextInt();
	   int limit_num = sc.nextInt();
	   
	   int[] array = new int[n];
	   
	   
	   for(int i = 0 ; i < n ; i++) {
		   array[i] = sc.nextInt();
	   }
	   
	  
	   
	   
	   for (int i = 0 ; i < array.length ; i++) {
		   if( array[i] < limit_num )
			   System.out.print(array[i] + " ");
	   }
	   
	    
    }
    
}

 

 

2.StringBuilder 사용



import java.util.Scanner;

public class Main{
       //BufferedReader를 사용하기위해선 예외처리가 필수
   public static void main(String[] args)  {
	   
	   Scanner sc = new Scanner(System.in);

	   int n  = sc.nextInt();
	   int limit_num = sc.nextInt();
	   
	   StringBuilder sb = new StringBuilder();
	   
	   for(int i =0 ; i < n ; i++) {
		   int num = sc.nextInt();
		   if (num < limit_num)
			   sb.append(num + " ");
		   
	   }
	   
	   System.out.println(sb);
	   
	    
    }
    
}

'프로그래밍언어 > 알고리즘' 카테고리의 다른 글

백준 2562 최댓값_JAVA  (0) 2023.05.06
백준 10818 최소, 최대_JAVA  (0) 2023.05.05
백준 10807 개수 세기_JAVA  (0) 2023.05.05
백준 10952 A+B-5_JAVA  (0) 2023.05.04
백준 2439_별 찍기2_JAVA  (0) 2023.05.04