(구) solved ac class 문제들/class 1

[class 1]백준 문자열 반복(2675번) 풀이 (C++/Python)

isekaipudding 2026. 4. 23. 13:45

문제 : 문자열 반복(2675번)(Bronze II)

문제 링크 : https://www.acmicpc.net/problem/2675
출처 : Baekjoon Online Judge

 

이 문제는 정말 간단합니다.

테스트 케이스 수 입력하고 그 다음 반복 횟수 int r과 문자열 string s를 입력 받아

s의 각 글자들을 r번 반복 출력하면 되는 문제입니다.

그러니 바로 소스 코드를 올리겠습니다.

#include <bits/stdc++.h>
 
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t;
    cin >> t;

    while(t--) {
        int r;
        cin >> r;
        string s;
        cin >> s;

        int size = s.size();

        for(int i = 0; i < size; i++) {
            char ch = s[i];
            for(int j = 0; j < r; j++) cout << ch;
        }

        cout << '\n';
    }

    return 0;
}

코딩에 익숙할 쯤 되면 이 문제가 쉽게 느껴질 것입니다.

 

Python은 구현 과정이 조금 특이합니다.

테스트 케이스 수 입력하는 것까지는 동일하나 L:list = list(map(str, input().split()))로 해서 R, S = int(L[0]), L[1]으로 해야 합니다.

왜냐면 Python은 한 줄의 문자열로 입력 받기 때문에 파싱 과정이 따로 필요합니다.

import sys

input = sys.stdin.readline

T:int = int(input().rstrip())

for _ in range(T) :
    L:list = list(map(str, input().split()))
    R, S = int(L[0]), L[1]
    
    result:str = ""
    
    for ch in S :
        result += R * ch
        
    print(result)