Intermedio 2 resueltos 100.0% de aceptación

Digit Power Sum

Add each digit raised to its position from the left.

Lo que aprenderás

  • 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

Problema

Ver traducción

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.

Restricciones

1 ≤ n ≤ 1,000,000,000

Ejemplos

Ejemplo 1
Entrada:
n = 352

Salida esperada:
expected = 36
Entrada:
long long n = 352;

Salida esperada:
long long expected = 36;
Entrada:
long n = 352;

Salida esperada:
long expected = 36;

Explicación

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

Ejemplo 2
Entrada:
n = 89

Salida esperada:
expected = 89
Entrada:
long long n = 89;

Salida esperada:
long long expected = 89;
Entrada:
long n = 89;

Salida esperada:
long expected = 89;

Explicación

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

Cómo abordarlo

Inténtalo primero por tu cuenta y luego abre las pistas una a una.

Pista 1

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

Pista 2

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

Pista 3

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

Complejidad esperada

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

Clasificación

Clasificación completa

La mejor solución aprobada de cada programador. Las más rápidas, ligeras y mejor valoradas van primero.

# Programador Lenguaje Tiempo Memoria Revisión de IA Solución
1 KKKaneki Ken Python 1,122 ms - 5/5 Ver código
2 JPJittipat Phonhan Go 1,486 ms - 4/5 Ver código

Aprende de otros

Soluciones de la comunidad

Alerta de spoiler: 2 soluciones completas abajo Intenta resolverlo tú mismo primero. Igual puedes ver el código de otros desarrolladores y las reseñas de IA. Inicia sesión y resuelve el reto para unirte a la discusión.