Tous les défis
Moyen 0 résolus 0 % de réussite

Number Letter Counts

Count the letters needed to write every number from 1 to n in English words.

Ce que vous allez apprendre

  • Spell numbers in English words
  • Split a number into thousands, hundreds, tens and ones
  • Count letters while ignoring spaces and hyphens

Problème

Afficher la traduction

A sign maker spells out house numbers in words and pays by the letter. Write every number from 1 to n in American English, without the word "and": 342 is "three hundred forty-two" and 2005 is "two thousand five". Spaces and hyphens are free; only letters count.

Implement number_letter_counts(n).

Spellings: one to nineteen, twenty, thirty, forty, fifty, sixty, seventy, eighty, ninety, hundred, thousand. Numbers from 1,000 up are written as the thousands part (itself in words, up to ninety-nine) followed by "thousand" and then the rest, if any.

Input

n: The last number to write.

Output

Return the total number of letters used for 1, 2, ..., n. For n = 0 nothing is written and the answer is 0.

Contraintes

0 ≤ n ≤ 99,999

Exemples

Exemple 1
Entrée :
n = 3

Sortie attendue :
expected = 11
Entrée :
long long n = 3;

Sortie attendue :
long long expected = 11;
Entrée :
long n = 3;

Sortie attendue :
long expected = 11;

Explication

"one", "two" and "three" have 3 + 3 + 5 = 11 letters.

Exemple 2
Entrée :
n = 21

Sortie attendue :
expected = 121
Entrée :
long long n = 21;

Sortie attendue :
long long expected = 121;
Entrée :
long n = 21;

Sortie attendue :
long expected = 121;

Explication

1 to 19 use 106 letters, twenty adds 6 and twenty-one adds 9, for 121.

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Handle 1 to 19 with a table, then the tens words for 20 to 90.

Indice 2

A three-digit block is the hundreds word plus "hundred" plus the last two digits.

Indice 3

For numbers of 1,000 and up, spell the thousands block, add "thousand", then spell the rest.

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.