알고리즘 BOJ

백준 2577 파이썬 | 숫자의 개수

콘2조아 2022. 4. 22. 17:58

코드

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

X=A*B*C
n0, n1, n2, n3, n4, n5, n6, n7, n8, n9 = [0]*10

for i in list(str(X)):

    if int(i) == 0:
        n0 += 1
    if int(i) == 1:
        n1 += 1
    if int(i) == 2:
        n2 += 1
    if int(i) == 3:
        n3 += 1 
    if int(i) == 4:
        n4 += 1
    if int(i) == 5:
        n5 += 1
    if int(i) == 6:
        n6 += 1
    if int(i) == 7:
        n7 += 1
    if int(i) == 8:
        n8 += 1
    if int(i) == 9:
        n9 += 1

print(n0)
print(n1)
print(n2)
print(n3)
print(n4)
print(n5)
print(n6)
print(n7)
print(n8)
print(n9)

설명

모든 0 - 9의 자연수에 대해 A*B*C의 값에서 하나하나 해당되는 수의 개수를 세는 방법으로 코드를 짰다.

 

메모리: 28776 KB

시간: 72 ms

 

하지만 더 간단한 방법이 있다.

n1 = int(input())
n2 = int(input())
n3 = int(input())

N = str( n1 * n2 * n3 )

for i in range(10):
    s = str(i)
    print(N.count(s))

출처: 

https://www.acmicpc.net/source/42309317

 

 

문제

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

 

2577번: 숫자의 개수

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

www.acmicpc.net