SWEA 1928 Base64 Decoder

Jmnote (토론 | 기여)님의 2019년 1월 29일 (화) 22:44 판

1 개요

SWEA 1928 Base64 Decoder
SW Expert 아카데미
# 문제 풀이

틀:SWEA 난이도 2-3

2 C++

#include <iostream>
using namespace std;
int m[128];
void init() {
    for(int i=0; i<26; i++) m['A'+i]=i;
    for(int i=0; i<26; i++) m['a'+i]=i+26;
    for(int i=0; i<11; i++) m['0'+i]=i+52;
    m['+'] = 62;
    m['/'] = 63;
}
void decode(char* src, char* dst) {
    int bits, pos=0, pos2=0;
    while(src[pos]) {
        bits = m[src[pos++]] << 18;
        bits += m[src[pos++]] << 12;
        bits += m[src[pos++]] << 6;
        bits += m[src[pos++]];
        dst[pos2++] = bits>>16 & 0xFF;
        dst[pos2++] = bits>>8 & 0xFF;
        dst[pos2++] = bits & 0xFF;
    }
    dst[pos2] = 0;
}
int main() {
    init();
    int T;
    scanf("%d", &T);
    char encoded[100000];
    char decoded[100000];
    for(int tc=1; tc<=T; tc++) {
        scanf("%s", encoded);
        decode(encoded, decoded);
        printf("#%d %s \n", tc, decoded);
    }
}

3 Java

import java.util.Scanner;
import java.util.Base64;
public class Solution {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int t=1; t<=T; t++) {
            String encoded = sc.next();
            String decoded = new String(Base64.getDecoder().decode(encoded));
            System.out.format("#%d %s\n", t, decoded);
        }
    }
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}