Problem
A spy number is a positive integer whose digit sum equals its digit product. For example, 1124 has sum 1 + 1 + 2 + 4 = 8 and product 1 × 1 × 2 × 4 = 8, so it is a spy number.
Implement spy_number(n).
Input
n: The number to test.
Output
Return true if the sum of the decimal digits of n equals their product, otherwise false. Every single-digit number is a spy number.
Einschränkungen
1 ≤ n ≤ 10^18
Beispiele
Eingabe:
n = 123
Erwartete Ausgabe:
expected = True
Eingabe:
long long n = 123;
Erwartete Ausgabe:
bool expected = true;
Eingabe:
long n = 123;
Erwartete Ausgabe:
bool expected = true;
Erläuterung
Sum 1 + 2 + 3 = 6 and product 1 × 2 × 3 = 6 are equal, so the answer is true.
Eingabe:
n = 1234
Erwartete Ausgabe:
expected = False
Eingabe:
long long n = 1234;
Erwartete Ausgabe:
bool expected = false;
Eingabe:
long n = 1234;
Erwartete Ausgabe:
bool expected = false;
Erläuterung
Sum 1 + 2 + 3 + 4 = 10 but product 1 × 2 × 3 × 4 = 24, so the answer is false.
So gehst du vor
Versuch es zuerst allein und öffne dann einen Hinweis nach dem anderen.
Hinweis 1
Walk over the digits once and update both totals.
Hinweis 2
Start the product at 1, not 0.
Hinweis 3
Any number containing a 0 digit has product 0, so it cannot be spy unless the sum is 0 too.
Erwartete Komplexität
O(d) time for d digits, O(1) extra space
Referenzen
Diese Aufgabe wurde für CodeUtility angepasst. Beachte die oben genannten Vorgaben und Einschränkungen.