Post

[BaekJoon] 11721번 - 열 개씩 끊어 출력하기 [Java][C++]

[BaekJoon] 11721번 - 열 개씩 끊어 출력하기 [Java][C++]

문제 링크


1. 문제 풀이


단어를 $10$ 글자 단위로 구분해서 출력하는 문제로 문자열에서 인덱스를 활용해 $10$ 으로 나눈 나머지가 $0$ 이 될 때를 기준으로 개행을 해주면 된다.


2. 코드


1. 풀이 [Java]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();

        String str = br.readLine();

        for (int i = 0; i < str.length(); i++) {
            if (i != 0 && i % 10 == 0) {
                sb.append("\n");
            }

            sb.append(str.charAt(i));
        }

        System.out.println(sb);
    }
}


2. 풀이 [C++]

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

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

    string s;
    cin >> s;

    for (int i = 0; i < s.size(); i++) {
        if (i != 0 && i % 10 == 0) {
            cout << '\n';
        }

        cout << s[i];
    }
}

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