모든 챌린지
보통 2명 해결 정답률 100.0%

Digit Power Sum

Add each digit raised to its position from the left.

배우게 될 내용

  • Split a number into its digits from left to right
  • Raise each digit to a power that depends on its position
  • Accumulate a total inside a loop

문제

번역본 보기

Number the digits of n from the left, starting at 1. Raise each digit to the power of its position and add the results. For 352 that is 3¹ + 5² + 2³. A zero digit adds 0.

Implement digit_power_sum(n).

Input

n: A positive integer.

Output

Return the sum of the powered digits.

제약 조건

1 ≤ n ≤ 1,000,000,000

예시

예시 1
입력:
n = 352

예상 출력:
expected = 36
입력:
long long n = 352;

예상 출력:
long long expected = 36;
입력:
long n = 352;

예상 출력:
long expected = 36;

설명

3¹ + 5² + 2³ = 3 + 25 + 8 = 36.

예시 2
입력:
n = 89

예상 출력:
expected = 89
입력:
long long n = 89;

예상 출력:
long long expected = 89;
입력:
long n = 89;

예상 출력:
long expected = 89;

설명

8¹ + 9² = 8 + 81 = 89, the number itself.

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

Scan the number digit by digit from left to right and keep track of each digit’s position.

힌트 2

Convert the number to a string or otherwise know the left-to-right index before combining each digit with its position.

힌트 3

Remember that a zero digit contributes nothing, but its position still matters for the exponent count.

예상 복잡도

O(d) time, O(1) extra space

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

# 코더 언어 시간 메모리 AI 평가 풀이
1 KKKaneki Ken Python 1,122 ms - 5/5 코드 보기
2 JPJittipat Phonhan Go 1,486 ms - 4/5 코드 보기

다른 사람의 풀이 보기

커뮤니티 솔루션

스포일러: 아래에 2개 전체 솔루션 있음 먼저 직접 풀어보세요. 다른 개발자의 코드와 AI 리뷰도 참고할 수 있습니다. 로그인하고 문제를 풀면 토론에 참여할 수 있습니다.