๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€/Java] 10871 X๋ณด๋‹ค ์ž‘์€ ์ˆ˜ (๋’ท๋ฌธ์ž ๊ณต๋ฐฑ ์ œ๊ฑฐ/๋ฌธ์ž์—ด ์•ž๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ)

by ๋ฝ€์งœ๊ผฌ 2025. 1. 19.
728x90
๋ฐ˜์‘ํ˜•

 

๋ฐฑ์ค€

๋ฌธ์ œ

์ •์ˆ˜ N๊ฐœ๋กœ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด A์™€ ์ •์ˆ˜ X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, A์—์„œ X๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ๋ชจ๋‘ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N๊ณผ X๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ N, X ≤ 10,000)

๋‘˜์งธ ์ค„์— ์ˆ˜์—ด A๋ฅผ ์ด๋ฃจ๋Š” ์ •์ˆ˜ N๊ฐœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ์ •์ˆ˜๋Š” ๋ชจ๋‘ 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.

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

์ถœ๋ ฅ

X๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ์ˆœ์„œ๋Œ€๋กœ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค. X๋ณด๋‹ค ์ž‘์€ ์ˆ˜๋Š” ์ ์–ด๋„ ํ•˜๋‚˜ ์กด์žฌํ•œ๋‹ค.

1 4 2 3

โœ๏ธ ์ •๋‹ต ์ฝ”๋“œ

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));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer Aitem = new StringTokenizer(br.readLine());

        int[] A = new int[N];
        for (int i = 0; i < N; i++) {
            A[i] = Integer.parseInt(Aitem.nextToken());
            if (A[i] < X){
                bw.write(A[i] + " ");
            }
        }
        bw.flush();
    }
}

 

๊ทธ๋Ÿฐ๋ฐ ์œ„์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๋ฉด

"1 4 2 3 "

์œผ๋กœ ๋’ค์— ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ๊ณผ ํ•จ๊ป˜ ์ถœ๋ ฅ์ด ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ

๋’ค์— ๋ถˆํ•„์š”ํ•œ ๊ณต๋ฐฑ์ด ์žˆ๋Š”๋ฐ ๋งž์•˜๋‹ค๊ณ  ์ฒ˜๋ฆฌ๊ฐ€ ๋œ๋‹ค..

์ œ๊ฑฐํ•ด์ค˜์•ผํ• ๊ฒƒ ๊ฐ™์€๋ฐ ์•„๋ฌด๋ฆฌ ๊ฒ€์ƒ‰ํ•ด๋„ ์ด๊ฑธ ์ฒ˜๋ฆฌํ•œ ์‚ฌ๋žŒ์ด ์•ˆ๋ณด์ž„. (ํ˜ผ๋ž€)

๊ทธ๋ž˜์„œ ๋‚˜ ํ˜ผ์ž ๋’ค์— ๊ณต๋ฐฑ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์•˜๋‹ค.

 

์™„๋ฒฝ์ฃผ์˜์ž๋Š” ์ด๋Ÿฐ๊ฑฐ ๋ชป์ฐธ์Œ


โœ๏ธ ๊ณต๋ฐฑ ์ œ๊ฑฐ ์ฝ”๋“œ

trim()์„ ์‚ฌ์šฉํ•˜๊ณ ์‹ถ์—ˆ๋‹ค.

(์•ž๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ์— ์‚ฌ์šฉํ•œ๋‹ค.)

 

๊ทธ๋Ÿฐ๋ฐ ๊ทธ๋ƒฅ bw.flush์—๋Š” ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ–ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ StringBuilder๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ •์ˆ˜๋“ค์„ append ์ถ”๊ฐ€์ถ”๊ฐ€ํ•œ๋‹ค์Œ์—!

trim์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

 

์ด๋ ‡๊ฒŒํ•˜๋ฉด ์•ž๋’ค ๊ณต๋ฐฑ ์ œ๊ฑฐ ์™„๋ฃŒ

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));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        StringTokenizer Aitem = new StringTokenizer(br.readLine());

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < N; i++) {
            int num = Integer.parseInt(Aitem.nextToken());
            if (num < X) {
                sb.append(num).append(" ");
            }
        }
        bw.write(sb.toString().trim());
        bw.flush();
    }
}

 

 

StringBuilder์— ๋Œ€ํ•œ ์„ค๋ช…์€

https://s2ej1n.tistory.com/91

 

[Java] StringBuilder๋ž€ ๋ฌด์—‡์ธ๊ฐ€? (๊ทธ๋ฆฌ๊ณ  String์˜ ๋ถˆ๋ณ€์„ฑ)

StringBuilder๋ฌธ์ž์—ด ์กฐ์ž‘์— ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค์ด๋‹ค.์™œ ์‚ฌ์šฉํ•˜๋Š”๊ฐ€?ํšจ์œจ์ ์ด๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉํ•œ๋‹ค.์ด๊ฒŒ ์™œ ํšจ์œจ์ ์ด๋ƒ๋ฅผ ์„ค๋ช…ํ•˜๋ ค๋ฉด๋ฌธ์ž์—ด์˜ ๋ถˆ๋ณ€ ํŠน์„ฑ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ์ดํ•ดํ•˜๊ณ  ๊ฐ€์•ผํ•œ๋‹ค. ๋ฌธ์ž์—ด Strin

s2ej1n.tistory.com

 

์ด๋ ‡๊ฒŒ ๋”ฐ๋กœ ์ •๋ฆฌํ•ด๋†“์•˜๋‹ค.

728x90
๋ฐ˜์‘ํ˜•