Post

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

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

문제 링크


1. 아이디어

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


2. 복잡도

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

($L$ = str의 길이, $N$ = 반복 횟수 n. 결과 문자열의 길이 $L \times N$. C++는 str을 출력 스트림에 $N$번 그대로 흘려보내 중간 버퍼가 없으므로 공간 $O(1)$)


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;
}
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.