sw사관학교 정글 2기/01 기초,재귀,완전탐색, 정렬
[기초-배열] 백준 2577번 숫자의 개수 with Python3
금의야행
2021. 8. 7. 22:44
https://www.acmicpc.net/problem/2577
2577번: 숫자의 개수
첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.
www.acmicpc.net
문제
세 개의 자연수 A, B, C가 주어질 때 A × B × C를 계산한 결과에 0부터 9까지 각각의 숫자가 몇 번씩 쓰였는지를 구하는 프로그램을 작성하시오.
내 풀이
a = int(input())
b = int(input())
c = int(input())
d = a*b*c
strd = str(d)
for i in range(10):
count = strd.count(f'{i}') # 굳이 f'' 이럴 필요는 없었다.
print(count)
해설
.count(self, x, __start, __end)
self는 무시하셔도 좋습니다. 심화적인 부분이라 생각이 들어서
x는 찾을 문자열, 찾을 문자를 넣으면 됩니다. __start, __end 는 예상하셨다 싶이 문자열의 어디부터 어디까지 내부에서 찾아달라는 뜻 입니다.
출처: https://blockdmask.tistory.com/410 [개발자 지망생]
정답 풀이
a = int(input())
b = int(input())
c = int(input())
total_str = str(a*b*c) # 숫자를 곱해서 str타입으로 변환
for num in range(10): # 0부터 9까지
num_count = total_str.count(str(num))
print(num_count)
#or
total = 1
for _ in range(3):
i = int(input())
total *= i # 3개의 정수를 곱함
total_str = str(total) # 숫자를 str타입으로 변환
for num in range(10): # 0부터 9까지
num_count = total_str.count(str(num))
print(num_count)
출처:https://ooyoung.tistory.com/56
해석