मध्यम 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

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।