Post

[BaekJoon] 21867번 - Java Bitecode [Java][C++]

[BaekJoon] 21867번 - Java Bitecode [Java][C++]

문제 링크


1. 문제 풀이


주어진 문자열에서 J, A, V를 뺀 나머지 문자들도 새로운 문자열을 만드는 방식으로 해결할 수 있다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();

        br.readLine();
        String str = br.readLine();

        for (char c : str.toCharArray()) {
            if (c == 'J' || c == 'A' || c == 'V') continue;
            sb.append(c);
        }

        if (sb.length() == 0) {
            System.out.println("nojava");
        } else {
            System.out.println(sb);
        }
    }
}


2. 풀이 [C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    string s;
    cin >> n >> s;

    string res;
    for (char c : s) {
        if (c == 'J' || c == 'A' || c == 'V') continue;
        res.push_back(c);
    }

    if (res.empty()) {
        cout << "nojava";
    } else {
        cout << res;
    }
}

This post is licensed under CC BY 4.0 by the author.