Einfach 0 gelöst 0% Erfolgsquote

Spy Number

Check whether the sum of a number's digits equals the product of its digits.

Das lernst du

  • Track a sum and a product in one pass
  • Start a product at 1 and a sum at 0
  • Notice how a 0 digit affects the product

Problem

Übersetzung anzeigen

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

Beispiel 1
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.

Beispiel 2
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.

Die beste bestandene Lösung jedes Coders. Schnellere, sparsamere und besser bewertete Lösungen stehen weiter oben.

Noch keine bestandene Lösung. Löse sie und hol dir Platz eins.

Von anderen lernen

Community-Lösungen

Noch keine veröffentlichten Lösungen. Löse die Aufgabe und teile als Erster eine Lösung.