Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 2742
- 나머지
- 두 수 비교하기
- 8958
- BOJ
- 별 찍기 - 1
- 기찍 N
- 수학
- 2741
- 10998
- A+B - 3
- A+B - 8
- 3052
- 10871
- 사칙연산
- A/B
- 2739
- 구현
- 2884
- A+B - 7
- 숫자의 개수
- 빠른 A+B
- A×B
- A+B - 5
- Python
- 별 찍기 - 2
- X보다 작은 수
- N 찍기
- 14681
- 사분면 고르기
Archives
- Today
- Total
RIRINTO's Blog
BOJ 2577: 숫자의 개수 (Python) 본문
첫째 줄에 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