Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Tags
more
Archives
Today
Total
관리 메뉴

걸음마부터 달리기

SWEA 1240 단순한 2진 암호코드 본문

카테고리 없음

SWEA 1240 단순한 2진 암호코드

성추 2025. 4. 11. 17:59

자존심 상한다. 이런 문제도 제대로 못맞추다니... 진짜 큰일이다

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV15FZuqAL4CFAYD&categoryId=AV15FZuqAL4CFAYD&categoryType=CODE&problemTitle=&orderBy=INQUERY_COUNT&selectCodeLang=ALL&select-1=3&pageSize=10&pageIndex=1#none

import java.util.*;
import java.io.*;

class Main {
    static int T;
    static int N;
    static int M;
    static String[] 암호코드 = new String[] {"0001101","0011001","0010011", "0111101","0100011","0110001","0101111","0111011","0110111","0001011"};
    static int[][] codeMap;
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        T = Integer.parseInt(br.readLine());
        for (int l = 0; l < T; l++) {
            System.out.printf("#%d ",(l+1));
            String[] s = br.readLine().split(" ");
            N = Integer.parseInt(s[0]);
            M = Integer.parseInt(s[1]);
            codeMap = new int[N][M];
            //암호 초기화
            for(int i = 0; i < N; i++) {
                char[] arr = br.readLine().toCharArray();
                for(int j = 0; j < M; j++) {
                    codeMap[i][j] = arr[j]-'0';
                }
            }
            int[] start = find1();
            int endC = start[1];
            int startR = start[0];
            int ans = check(startR, endC);
            System.out.println(ans);


        }
    }
    public static int[] find1(){
        for(int i=0; i<N ; i++){
            for(int j=M-1; j>=0; j--){
                if(codeMap[i][j]==1){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }


    public static int check (int startR , int endC){
        String[] 문자 = new String[8];
        int[] decode = new int[8];
        for(int i=0; i<8; i++){
            String temp = "";
            for(int j=0; j<7; j++){
                temp=codeMap[startR][endC-j]+temp;
            }
            문자[7-i] = temp;
            endC-=7;
        }
        for(int i=0; i<8; i++){
            for(int j=0; j<암호코드.length; j++){
                if(문자[i].equals(암호코드[j])){
                    decode[i] = j;
                    break;
                }
            }
        }
        int sum = (decode[0] +decode[2] +decode[4] +decode[6])*3 + (decode[1]+decode[3]+decode[5]+decode[7]);
        if(sum%10==0){
            return decode[0] +decode[2] +decode[4] +decode[6]+decode[1]+decode[3]+decode[5]+decode[7];
        }
        else{
            return 0;
        }
    }

}

통과한 내 코드이다. 

매번 느끼는거지만 다른 사람들이 50줄이면 짤것을 나는 항상 *2 , *3 만큼 더 짰다는 것이다.

이러다보니 코드 량이 너무 많아서 항상 한번에 통과를 못하고 디버깅을 했었어야했다.

import java.util.*;
class Solution{
    public static void main(String args[]) throws Exception {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
        HashMap<String, Integer> map = new HashMap<>();
        String[] nums = {"0001101", "0011001", "0010011","0111101","0100011","0110001","0101111","0111011","0110111","0001011"};
        for(int i = 0; i < nums.length; i++){
            map.put(nums[i],i);
        }
		for(int tc = 1; tc <= T; tc++) {
            int n = sc.nextInt();
            int m = sc.nextInt();
            String[] info = new String[n];
            for(int i = 0; i < n; i++){
                info[i] = sc.next();
            }
            int[] answer = new int[8];
            int count = 7;
            int even = 0;
            int odd = 0;
            for(int i = 0; i < n; i++){
                if(!info[i].contains("1")) continue;
                int idx = info[i].lastIndexOf("1") + 1;
                while(count != -1){
                    answer[count] = map.get(info[i].substring(idx - 7,idx));
                    if(count % 2 == 0){
                        odd += answer[count];
                    }else{
                        even += answer[count];
                    }
                    count--;
                    idx -= 7;
                }
                break;
            }
            int result = odd + even;
            if((odd * 3 + even) % 10 != 0){
                result = 0;
            }
            System.out.println("#" + tc + " " + result);
        }
	}
}

substring을 활용하자.

lastIndexOf를 활용하자. 

contains를 활용하자.

 

이것들을 안쓰면 for문으로 계속 탐색해야되어서 코드가 길어질 수 밖에 없다.