Post

[Programmers] 181950번 - 문자열 반복해서 출력하기 [Java][C++]

[Programmers] 181950번 - 문자열 반복해서 출력하기 [Java][C++]

문제 링크


1. 아이디어

입력 문자열을 n번 반복해서 출력해주면 된다.


2. 복잡도

시간복잡도공간복잡도
$O(L \times R)$$O(1)$

$L$ = str 길이, $R$ = 반복 횟수 n


3. 코드

풀이 [Java][C++]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.io.*;
import java.util.*;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        String s = st.nextToken();
        int n = Integer.parseInt(st.nextToken());

        System.out.println(s.repeat(n));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);

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

    while (n--) {
        cout << s;
    }
}

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