🌞Algorithm/🔥Baekjoon

[Baekjoon] 6212_Dream Counting

뿌야._. 2025. 5. 8. 12:01
문제(출처: https://www.acmicpc.net/problem/6212)

< Dream Counting >

 

문제 풀이 

 

M부터 N까지 각 자릿수의 값이 등장한 횟수를 HashMap을 사용하여 구한다.

 

my solution (Java)

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.StringTokenizer;

public class _6212_ { // Dream Counting

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		StringTokenizer st = new StringTokenizer(bf.readLine());

		int M = Integer.parseInt(st.nextToken());
		int N = Integer.parseInt(st.nextToken());

		HashMap<Integer, Integer> map = new HashMap<>();
		for (int i = 0; i <= 9; i++) {
			map.put(i, 0);
		}

		for (int i = M; i <= N; i++) {
			String str = Integer.toString(i);

			for (int j = 0; j < str.length(); j++) {
				map.replace(str.charAt(j) - '0', map.get(str.charAt(j) - '0') + 1);
			}
		}

		for (int i = 0; i <= 9; i++) {
			bw.write(map.get(i) + " ");
		}
		bw.flush();
	}
}
변수)
M, N : 입력값
map : HashMap <Integer, Integer>

 

M, N을 입력받는다. HashMap에 key 값으로 0부터 9까지 저장한다. M부터 N까지 살펴보며 각 자릿수의 값을 HashMap에 count 한다. 최종 HashMap의 값을 출력한다.



 

'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글

[Baekjoon] 15464_The Bovine Shuffle  (0) 2025.05.12
[Baekjoon] 9626_크로스워드 퍼즐  (0) 2025.05.09
[Baekjoon] 11999_Milk Pails (Bronze)  (1) 2025.05.07
[Baekjoon] 5976_A spiral walk  (1) 2025.05.02
[Baekjoon] 11809_YODA  (1) 2025.05.01