Post

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

문자열 str을 n번 반복해 출력하는 워밍업 문제.

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

문제 링크


1. 아이디어

문자열 str과 반복 횟수 n을 입력받아 strn번 이어붙여 출력하면 되는 문제다.


2. 복잡도

접근시간공간
풀이$O(N \times K)$$O(N \times K)$

($N$ = str의 길이, $K$ = 반복 횟수 n)


3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
11
12
13
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
#include <bits/stdc++.h>
using namespace std;

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

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

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

C++ 표준 라이브러리엔 Java의 repeat, Python의 *에 대응하는 문자열 반복 연산이 없어 n번 반복문을 돌며 출력해줬다.

1
2
3
4
5
6
import sys

input = sys.stdin.readline

s, n = input().split()
print(s * int(n))

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