[백준 브론즈 V] 10871번: X보다 작은 수

DONI·2021년 8월 7일
0

Baekjoon Online Judge

목록 보기
26/31
post-thumbnail

문제

정수 N개로 이루어진 수열 A와 정수 X가 주어진다. 이때, A에서 X보다 작은 수를 모두 출력하는 프로그램을 작성하시오.


입력

첫째 줄에 N과 X가 주어진다. (1 ≤ N, X ≤ 10,000)

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


출력

X보다 작은 수를 입력받은 순서대로 공백으로 구분해 출력한다. X보다 작은 수는 적어도 하나 존재한다.


예제 입력 1

10 5
1 10 4 9 2 3 8 5 7 6

예제 출력 1

1 4 2 3


소스코드

  • Java 첫 번째 방법 : for 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int x = sc.nextInt();
		for (int i = 0; i < a; i++) {
			int n = sc.nextInt();
			if (n < x) System.out.print(n + " ");
		} sc.close();
	}
}
  • Java 두 번째 방법 : while 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int x = sc.nextInt();
		int i = 0;
		while (i < a) {
			int n = sc.nextInt();
			if (n < x) System.out.print(n + " ");
			i++;
		} sc.close();
	}
}
  • Java 세 번째 방법 : do-while 문
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int x = sc.nextInt();
		int i = 0;
		do {
			int n = sc.nextInt();
			if (n < x) System.out.print(n + " ");
			i++;
		} while (i < a);
		sc.close();
	}
}

[바로가기] 10871번: X보다 작은 수

profile
틀린 내용이 있다면 댓글 또는 이메일로 알려주세요 ❤ꔛ❜

0개의 댓글