Средне Решено: 0 Процент решений: 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

Учитесь у других

Решения сообщества

Пока нет опубликованных решений. Решите задание, чтобы поделиться первым.