Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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 31
Archives
Today
Total
관리 메뉴

RIRINTO's Blog

BOJ 2577: 숫자의 개수 (Python) 본문

BOJ

BOJ 2577: 숫자의 개수 (Python)

RIRINTO 2021. 9. 19. 15:45

BOJ 2577: 숫자의 개수

http://boj.kr/2577

 

2577번: 숫자의 개수

첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.

www.acmicpc.net


첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C를 입력받고 세 수를 모두 곱한 후, 0 ~ 9의 숫자가 몇 개 포함되어있는지 차례로 출력합니다.


더보기

각 자리의 수를 카운트하여 결과를 구할 수 있습니다.

A = int(input())
B = int(input())
C = int(input())

product = A * B * C
numbers = {}
while product:
    numbers[product % 10] = numbers.get(product % 10, 0) + 1
    product = product // 10

for i in range(10):
    print(numbers.get(i, 0))

 

collections 라이브러리의 Counter를 사용하여 더욱 쉽게 구할 수 있습니다.

from collections import Counter

A = int(input())
B = int(input())
C = int(input())

product = A * B * C
numbers = Counter(map(int, list(str(product))))

for i in range(10):
    print(numbers.get(i, 0))

 

물론 str 자료형을 거치지 않고도 표현이 가능하죠.

from collections import Counter


def counter(num):
    arr = []
    while num:
        arr.append(num % 10)
        num = num // 10
    return arr


A = int(input())
B = int(input())
C = int(input())

numbers = Counter(counter(A * B * C))

for i in range(10):
    print(numbers.get(i, 0))

 

'BOJ' 카테고리의 다른 글

BOJ 1546: 평균 (Python)  (0) 2021.09.19
BOJ 3052: 나머지 (Python)  (0) 2021.09.19
BOJ 2562: 최댓값 (Python)  (0) 2021.09.19
BOJ 10818: 최소, 최대 (Python)  (0) 2021.09.19
BOJ 1110: 더하기 사이클 (Python)  (0) 2021.09.19
Comments