Dado un número, imprime palabras para dígitos individuales. No está permitido usar if o switch .
Ejemplos:
Input: n = 123 Output: One Two Three Input: n = 350 Output: Three Five Zero
Le recomendamos encarecidamente que minimice su navegador y que pruebe esto usted mismo primero.
La idea es utilizar una array de strings para almacenar asignaciones de dígitos a palabras. A continuación se muestran los pasos.
Deje que el número de entrada sea n.
- Cree una array de strings para almacenar la asignación de dígitos a palabras.
- Cree otra array digits[] para almacenar dígitos individuales de n.
- Recorra los dígitos de n y guárdelos en digits[]. Tenga en cuenta que la forma estándar de recorrido mediante el almacenamiento repetido de n%10 y haciendo n = n/10, atraviesa los dígitos en orden inverso.
- Recorra la array de dígitos desde el final hasta el principio e imprima palabras usando el mapeo creado en el paso 1.
A continuación se muestra la implementación de la idea anterior.
C++
// C++ program to print individual words without if and // without switch #include <bits/stdc++.h> using namespace std; // To store digit to word mapping char word[][10] = {"zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"}; void printWordsWithoutIfSwitch(int n) { // Store individual digits int digits[10]; // a 32 bit int has at-most 10 digits int dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n%10; n = n/10; dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (int i=dc-1; i>=0; i--) cout << word[digits[i]] << " "; } // Driver program int main() { int n = 350; printWordsWithoutIfSwitch(n); return 0; }
Java
// Java program to print individual words without // if and without switch class GFG { // To store digit to word mapping static String word[] = {"zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"}; static void printWordsWithoutIfSwitch(int n) { // Store individual digits int digits[] = new int[10]; // a 32 bit int has at-most 10 digits int dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n % 10; n = n/10; dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (int i = dc - 1; i >= 0; i--) System.out.print(word[digits[i]] + " "); } // Driver program public static void main(String[] args) { int n = 350; printWordsWithoutIfSwitch(n); } } // This code has been contributed by 29AjayKumar
C#
// C# program to print individual words without // if and without switch using System; class GFG { // To store digit to word mapping static String []word = {"zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"}; static void printWordsWithoutIfSwitch(int n) { // Store individual digits int []digits = new int[10]; // a 32 bit int has at-most 10 digits int dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n % 10; n = n/10; dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (int i = dc - 1; i >= 0; i--) Console.Write(word[digits[i]] + " "); } // Driver program public static void Main(String[] args) { int n = 350; printWordsWithoutIfSwitch(n); } } // This code contributed by Rajput-Ji
Python3
# Python program to print individual words without if and # without switch # To store digit to word mapping word= ["zero", "one", "two", "three","four","five", "six", "seven", "eight", "nine"] def printWordsWithoutIfSwitch(n): # Store individual digits digits = [0 for i in range(10)] # a 32 bit has at-most 10 digits dc = 0 # Initialize digit count for given number 'n' # The below loop stores individual digits of n in # reverse order. do-while is used to handle "0" input while True: digits[dc] = n%10 n = n//10 dc += 1 if(n==0): break # Traverse individual digits and print words using # word[][] for i in range(dc-1,-1,-1): print(word[digits[i]],end=" ") # Driver program n = 350 printWordsWithoutIfSwitch(n) # This code is contributed by mohit kumar 29
Javascript
<script> // Javascript program to print individual words without // if and without switch // To store digit to word mapping let word=["zero", "one", "two", "three","four", "five", "six", "seven", "eight", "nine"]; function printWordsWithoutIfSwitch(n) { // Store individual digits let digits = new Array(10); // a 32 bit int has at-most 10 digits let dc = 0; // Initialize digit count for given number 'n' // The below loop stores individual digits of n in // reverse order. do-while is used to handle "0" input do { digits[dc] = n % 10; n = Math.floor(n/10); dc++; } while (n != 0); // Traverse individual digits and print words using // word[][] for (let i = dc - 1; i >= 0; i--) document.write(word[digits[i]] + " "); } // Driver program let n = 350; printWordsWithoutIfSwitch(n); // This code is contributed by unknown2108 </script>
three five zero
Complejidad de tiempo: O(|n|), donde |n| es el número de dígitos en el número dado n.
Espacio auxiliar: O (1), para almacenar el dígito a la asignación de palabras
Gracias a Utkarsh Trivedi por sugerir la solución anterior.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA