SWEA-1928 Base64 Decorder

‍bng4535·2022년 11월 16일
0

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PR4DKAG0DFAUq

설명
Base64 라이브러리를 활용하면 간단하다.

내 풀이

import java.util.Scanner;
class Solution
{
    public static void main(String args[]) throws Exception
    {
     
 
     
        Scanner sc = new Scanner(System.in);
        int T;
        T=sc.nextInt();
        sc.nextLine();
 
        for(int test_case = 1; test_case <= T; test_case++)
        {
        // -65 
            String s= sc.nextLine(); 
            String entireString = "";
            for(int i=0; i<s.length(); i++){
                char ch = s.charAt(i); 
                if(ch>='A' && ch<='Z') ch -=65;
                else if(ch>='a' && ch<='z') ch -=71;
                else if (ch>='0' && ch<='9') ch +=4; 
                else if (ch =='+') ch +=19;
                else if (ch =='/') ch +=16;
                String num = Integer.toString(ch ,2); 
                int len = num.length();
                for(int k=0;k<6-len; k++){
                    num ="0"+num; 
                }
                entireString+= num; 
            }
            String origin ="";
            for(int i=0; i<entireString.length(); i+=8){
                String temp = entireString.substring(i,i+8);
                int a= Integer.parseInt(temp,2);
                origin+= (char) a;
            }
            System.out.println("#"+test_case+" "+origin);
 
        }
    }
}
	

Base64 라이브러리를 활용한 풀이

import java.util.Arrays;
import java.util.Base64;
import java.util.Scanner;

public class Solution {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		sc.nextLine();
		
		for(int tc = 1; tc<=T; tc++) {
			String encode = sc.nextLine();
			String decode = new String(Base64.getDecoder().decode(encode));
            System.out.printf("#%d %s\n", tc, decode);
		}
	}
}
profile
공부 기록

0개의 댓글