Dada una array arr[] de N elementos que están en el rango [0, 9]. la tarea es convertir cada elemento de la array en sus strings numéricas.
Ejemplos:
Entrada: arr[] = [1, 4, 3, 2, 6]
Salida : uno cuatro tres dos seisEntrada: arr[]= [0, 4, 4, 6, 9]
Salida: cero cuatro cuatro seis nueve
Enfoque: El problema se puede resolver con la ayuda de map . Siga los pasos que se indican a continuación:
- Asigne cada número del 1 al 9 a sus respectivas strings numéricas.
- Itere a través de la array y cambie cada número a la string a la que está asignado.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to convert // the numeric array to words void convert(int arr[], int N) { map<int, string> mp; int i; // Map the integers to // their respective numeric string mp[0] = "zero"; mp[1] = "one"; mp[2] = "two"; mp[3] = "three"; mp[4] = "four"; mp[5] = "five"; mp[6] = "six"; mp[7] = "seven"; mp[8] = "eight"; mp[9] = "nine"; // Traverse array elements and print for (i = 0; i < N; i++) cout << mp[arr[i]] << " "; cout << endl; } // Driver Code int main() { int arr[] = { 1, 4, 3, 2, 6 }; int N = sizeof(arr) / sizeof(arr[0]); // Function call convert(arr, N); return 0; }
Java
// JAVA code to implement the approach import java.util.*; class GFG { // Function to convert // the numeric array to words public static void convert(int arr[], int N) { HashMap<Integer, String> mp = new HashMap<>(); int i; // Map the integers to // their respective numeric string mp.put(0, "zero"); mp.put(1, "one"); mp.put(2, "two"); mp.put(3, "three"); mp.put(4, "four"); mp.put(5, "five"); mp.put(6, "six"); mp.put(7, "seven"); mp.put(8, "eight"); mp.put(9, "nine"); // Traverse array elements and print for (i = 0; i < N; i++) System.out.print(mp.get(arr[i]) + " "); System.out.println(); } // Driver Code public static void main(String[] args) { int arr[] = new int[] { 1, 4, 3, 2, 6 }; int N = arr.length; // Function call convert(arr, N); } } // This code is contributed by Taranpreet
Python3
# Python code for the above approach # Function to convert # the numeric array to words def convert(arr, N): mp = {}; # Map the integers to # their respective numeric string mp[0] = "zero"; mp[1] = "one"; mp[2] = "two"; mp[3] = "three"; mp[4] = "four"; mp[5] = "five"; mp[6] = "six"; mp[7] = "seven"; mp[8] = "eight"; mp[9] = "nine"; # Traverse array elements and print for i in range(N): print(mp[arr[i]], end = " "); # Driver Code arr = [1, 4, 3, 2, 6]; N = len(arr); # Function call convert(arr, N); # This code is contributed by Potta Lokesh
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Function to convert // the numeric array to words static void convert(int []arr, int N) { Dictionary<int, string> mp = new Dictionary<int, string>(); int i; // Map the integers to // their respective numeric string mp[0] = "zero"; mp[1] = "one"; mp[2] = "two"; mp[3] = "three"; mp[4] = "four"; mp[5] = "five"; mp[6] = "six"; mp[7] = "seven"; mp[8] = "eight"; mp[9] = "nine"; // Traverse array elements and print for (i = 0; i < N; i++) Console.Write(mp[arr[i]] + " "); Console.WriteLine(); } // Driver Code public static void Main() { int []arr = { 1, 4, 3, 2, 6 }; int N = arr.Length; // Function call convert(arr, N); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code to implement the approach // Function to convert // the numeric array to words const convert = (arr, N) => { let mp = {}; let i; // Map the integers to // their respective numeric string mp[0] = "zero"; mp[1] = "one"; mp[2] = "two"; mp[3] = "three"; mp[4] = "four"; mp[5] = "five"; mp[6] = "six"; mp[7] = "seven"; mp[8] = "eight"; mp[9] = "nine"; // Traverse array elements and print for (i = 0; i < N; i++) document.write(`${mp[arr[i]]} `); document.write("<br/>"); } // Driver Code let arr = [1, 4, 3, 2, 6]; let N = arr.length; // Function call convert(arr, N); // This code is contributed by rakeshsahni </script>
Producción
one four three two six
Complejidad temporal: O(N).
Espacio Auxiliar: O(N).