Dado un entero grande como una string str , la tarea es encontrar el número de fósforos necesarios para representarlo.
Ejemplos:
Entrada: str = «56»
Salida: 11
Se requieren 5 palos para representar 5 y
6 palos para representar 6.
Entrada: str = «548712458645878»
Salida: 74
Enfoque: almacene el recuento de fósforos necesarios para representar cada dígito del 0 al 9 en una array de palos []. Ahora recorra la string dada dígito por dígito y agregue la cantidad de palos requeridos para el dígito actual.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // stick[i] stores the count of sticks // required to represent the digit i const int sticks[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // Function to return the count of // matchsticks required to represent // the given number int countSticks(string str, int n) { int cnt = 0; // For every digit of the given number for (int i = 0; i < n; i++) { // Add the count of sticks required // to represent the current digit cnt += (sticks[str[i] - '0']); } return cnt; } // Driver code int main() { string str = "56"; int n = str.length(); cout << countSticks(str, n); return 0; }
Java
// Java implementation of the approach import java.util.*; class GFG { // stick[i] stores the count of sticks // required to represent the digit i static int sticks[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // Function to return the count of // matchsticks required to represent // the given number static int countSticks(String str, int n) { int cnt = 0; // For every digit of the given number for (int i = 0; i < n; i++) { // Add the count of sticks required // to represent the current digit cnt += (sticks[str.charAt(i) - '0']); } return cnt; } // Driver code public static void main(String []args) { String str = "56"; int n = str.length(); System.out.println(countSticks(str, n)); } } // This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach # stick[i] stores the count of sticks # required to represent the digit i sticks = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 ]; # Function to return the count of # matchsticks required to represent # the given number def countSticks(string, n) : cnt = 0; # For every digit of the given number for i in range(n) : # Add the count of sticks required # to represent the current digit cnt += (sticks[ord(string[i]) - ord('0')]); return cnt; # Driver code if __name__ == "__main__" : string = "56"; n = len(string); print(countSticks(string, n)); # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // stick[i] stores the count of sticks // required to represent the digit i static int []sticks = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 }; // Function to return the count of // matchsticks required to represent // the given number static int countSticks(String str, int n) { int cnt = 0; // For every digit of the given number for (int i = 0; i < n; i++) { // Add the count of sticks required // to represent the current digit cnt += (sticks[str[i] - '0']); } return cnt; } // Driver code public static void Main(String []args) { String str = "56"; int n = str.Length; Console.WriteLine(countSticks(str, n)); } } // This code is contributed by 29AjayKumar
Javascript
<script> // Javascript implementation of the approach // stick[i] stores the count of sticks // required to represent the digit i var sticks = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 ] // Function to return the count of // matchsticks required to represent // the given number function countSticks(str, n) { var cnt = 0; // For every digit of the given number for (var i = 0; i < n; i++) { // Add the count of sticks required // to represent the current digit cnt += (sticks[str[i] - '0']); } return cnt; } // Driver code var str = "56"; var n = str.length; document.write(countSticks(str, n)); // This code is contributed by rutvik_56. </script>
Producción:
11
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)