"SWEA 1946 간단한 압축 풀기"의 두 판 사이의 차이

66번째 줄: 66번째 줄:
     }
     }
}
}
</source>
==Python==
<source lang='python'>
T = int(input())
for t in range(1, T+1):
    N = int(input())
    document = ''
    for j in range(N):
        Ci, Ki = input().split()
        Ki = int(Ki)
        while True:
            if Ki <= 0:
                break
            document += Ci
            Ki -= 1
    print('#{}'.format(t))
    count = 1
    for c in document:
        print(c, end='')
        if count % 10 == 0:
            print()
        count += 1
    print()
# github.com/wansang93/Algorithm
</source>
</source>

2020년 6월 3일 (수) 09:33 판

1 개요

SWEA 1946 간단한 압축 풀기
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 2-2

2 C++

#include <iostream>
using namespace std;
int main() {
    int T;
    scanf("%d", &T);
    int N, K;
    char C;
    char a[200];
    for(int tc=1; tc<=T; tc++) {
        scanf("%d", &N);
        int pos = 0;
        for(int i=0; i<N; i++) {
            scanf(" %c", &C);
            scanf("%d", &K);
            for(int j=0; j<K; j++) {
                a[pos+j] = C;
            }
            pos += K;
        }
        printf("#%d\n", tc);
        for(int i=0; i<pos; i+=10) {
            int end = i+10;
            if( pos < end ) end = pos;
            for(int j=i; j<end; j++) printf("%c", a[j]);
            printf("\n");
        }
    }
}

3 Java

import java.util.Scanner;
public class Solution {
    public static String str_repeat(String str, int times) {
        return new String(new char[times]).replace("\0", str);
    }
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int t=1; t<=T; t++) {
            int n = sc.nextInt();
            String s = "";
            for(int i=0; i<n; i++) {
                String ch = sc.next();
                int r = sc.nextInt();
                s += str_repeat(ch, r);    
            }
            System.out.format("#%d\n", t);
            for(int i=0; i<s.length(); i++) {
                System.out.print(s.charAt(i));
                if( i%10 == 9 )System.out.print("\n");
            }
            System.out.print("\n");
        }
    }
}

4 Python

T = int(input())
for t in range(1, T+1): 
    N = int(input())
    document = ''
    for j in range(N):
        Ci, Ki = input().split()
        Ki = int(Ki)
        while True:
            if Ki <= 0:
                break
            document += Ci
            Ki -= 1

    print('#{}'.format(t))
    count = 1
    for c in document:
        print(c, end='')
        if count % 10 == 0:
            print()
        count += 1
    print()

# github.com/wansang93/Algorithm
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}