본문 바로가기

파이썬 알고리즘 (인프런)/코드 구현력 기르기

6. 자릿수의 합

# 자릿수의 합

def run(nums):
    max_s, num = 0, 0
    for n in nums:
        s = 0
        for ch in str(n):
            s += int(ch)

        if s > max_s:  # 최대일때 숫자
            max_s = s
            num = n

    return num


def run(nums):
    max_s, idx = 0, 0
    for i, n in enumerate(nums):
        s = 0
        while n > 0:
            s += n % 10
            n //= 10

        if s > max_s:  # 최대일때 인덱스
            max_s = s
            idx = i

    return nums[idx]


N = int(input())
nums = list(map(int, input().split()))
print(run(nums))

'파이썬 알고리즘 (인프런) > 코드 구현력 기르기' 카테고리의 다른 글

8. 뒤집은 소수  (0) 2021.01.06
7. 소수 (에라토스테네스 체)  (0) 2021.01.06
5. 정다면체  (0) 2021.01.06
4. 대표값  (0) 2021.01.06
3. K번째 큰 수  (0) 2021.01.06