현재

12. 암호 본문

알고리즘/기타알고리즘문제

12. 암호

AAAge 2024. 4. 9. 19:35

설명

반복되는 횟수 입력 길이는 전부 7, #은 1 *은 0 이진수로 출력후 이거를 아스키 코드로 변환하라

예시 입력 1 

4
#****###**#####**#####**##** 

예시 출력 1

COOL

 

<힌트>

더보기

 .replace / substring 을 사용하는게 훨신 간단하다..

 

<자체풀이>

더보기
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        String input = sc.next();
        String answer = "";
        int check[] = new int[7*a];
        for(int i = 0; i < a ; i++){
            int result = 0;
            int count = 6;
            for(int j = (i-0)*7 ; j < 7*(i+1); j++){
                if("#".equals(String.valueOf(input.charAt(j)))){
                    check[j] = 1;
                    result += (int) Math.pow(2, count);
                }else{
                    check[j] = 0;
                }
                count--;
            }
            System.out.print((char)result);
        }

        // for(int e : check){
        //     System.out.print(e+" ");
        // }

        //4
        //#****## #**#### #**#### #**##**

        //#****###**#####**#####**##**
        //COOL

        sc.close();
    }
}

<정답코드>

더보기
import java.util.*;
class Main {    
    public String solution(int n, String s){
        String answer="";
        for(int i=0; i<n; i++){
            String tmp=s.substring(0, 7).replace('#', '1').replace('*', '0');
            int num=Integer.parseInt(tmp, 2);
            answer+=(char)num;
            s=s.substring(7);
        }
        return answer;
    }

    public static void main(String[] args){
        Main T = new Main();
        Scanner kb = new Scanner(System.in);
        int n=kb.nextInt();
        String str=kb.next();
        System.out.println(T.solution(n, str));
    }
}

'알고리즘 > 기타알고리즘문제' 카테고리의 다른 글

뒤집은 소수  (0) 2024.04.16
소수(에라토스테네스 체)  (0) 2024.04.16
11. 문자열 압축  (0) 2024.04.08
10. 가장 짧은 문자거리  (0) 2024.04.08
8. 유효한 팰린드롬  (0) 2024.04.03