[백준/파이썬] 13225번 풀이

업데이트:



문제 정보


풀이

문제

Given an integer n, compute the number of divisors of n.

A divisor is an integer, d (1 <= d <= n) that evenly divides n.

Example: If n=10, divisors are: 1, 2, 5 and 10. So the result would be 4.

Example: If n=104717, divisors are 1 and 104717. This is a prime number so the number of divisors is 2.

입력 요약
The first line contains an integer C (1 <= C <= 10) with the amount of numbers you need to process. The next C lines will contain an integer n (1 <= n < 10000) each. You have to compute the number of divisors of these values.

출력 요약
For each integer n, print a line with the number n itself, a space and the number of divisors.

코드

for T in range(int(input())):
    n=int(input())
    r=set()
    for i in range(1,int(n**.5)+1):
        if n%i==0:
            r.add(i)
            r.add(n//i)
    print(n, len(r))

설명

핵심은 구현 관점에서 Given an integer n, compute the number of divisors of n.

A divisor is an integer, d (1 <= d <= n) that evenly divides n. …를 만족하도록 로직을 구성하는 것입니다.

코드는 입력을 파싱한 뒤 조건 분기와 계산을 순서대로 수행하고, 문제에서 요구한 형식으로 결과를 출력합니다.

경계값과 예외 케이스도 함께 고려해 오답이 나기 쉬운 상황을 방지합니다.



다음 읽을거리

관련 허브 페이지에서 같은 주제의 글을 이어서 확인할 수 있습니다.

댓글남기기