Java/Java 알고리즘

백준 15312번 이름 궁합 JAVA 구현해보기

kimc 2022. 6. 2. 20:20

```

백준 15312번 이름 궁합 JAVA 구현해보기

```

이번 글을 통해 배워갈 내용

  1. 백준 15312번 이름 궁합 풀이

https://www.acmicpc.net/problem/15312

 

15312번: 이름 궁합

영어 대문자 알파벳 26개의 획수는 순서대로 3, 2, 1, 2, 3, 3, 2, 3, 3, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1 로 정한다. (출제자가 알파벳 대문자를 쓰는 방법이 기준이다)

www.acmicpc.net

 

백준 15312번 이름 궁합은

난이도 브론즈 등급의 문제로서

 

길이가 같은

남자 이름 A

여자 이름 B 가 주어질 경우

두 이름을 한 글자씩 번갈아 쓰고 각 자릿수 획의 수를 구하고

인접한 숫자끼라 더한 일의 자리값을 구해준다음

반복해서

마지막 두 자리를 구해주면 되는 문제입니다.

 


30분 정도 위에 링크를 방문하셔서 풀어보시고

안 풀리시는 경우에만 아래 해답을 봐주시면 감사하겠습니다.


일단 겹친 문자열에 각 문자에 획의 수의 리스트를 구해준 다음

해당 리스트가 두 자릿수가 될 때까지 인접한 각 자릿수의 합에 일의 자리 값을 구하고

결괏값을 출력합니다.

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

public class Main {

    public static void main(String[] args) throws IOException {
        final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        final String hisName = br.readLine();
        final String herName = br.readLine();
        System.out.print(solution(hisName, herName));
    }

    private static String solution(String hisName, String herName) {
        List<Integer> combinedList = new ArrayList<>();
        final int[] strokeArr = new int[]{3, 2, 1, 2, 3, 3, 2, 3, 3, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1};
        IntStream.range(0, hisName.length()).forEach(idx -> {
            combinedList.add(strokeArr[hisName.charAt(idx) - 'A']);
            combinedList.add(strokeArr[herName.charAt(idx) - 'A']);
        });
        do {
            List<Integer> tempList = new ArrayList<>();
            IntStream.range(0, combinedList.size() - 1).forEach(idx ->
                    tempList.add((combinedList.get(idx) + combinedList.get(idx + 1)) % 10));
            combinedList.clear();
            combinedList.addAll(tempList);
        } while (combinedList.size() != 2);

        StringBuilder sb = new StringBuilder();
        combinedList.forEach(sb::append);
        return sb.toString();
    }

}

 

읽어주셔서 감사합니다

 

무엇인가 얻어가셨기를 바라며

 

오늘도 즐거운 코딩 하시길 바랍니다 ~ :)

 


 

728x90