Dado un número n. Cuenta el número de cambios en la luz LED cuando se muestra uno tras otro de un número dado. (Al principio todos los LED están apagados). El número se ingresa en forma de string.
Vea esta imagen de la pantalla de siete segmentos para una mejor comprensión.
Ejemplos:
Input : n = "082" Output : 9 We need 6 LED lights to display 0 in seven segment display. We need 7 lights for 8 and 5 lights for 2. So total on/off is 6 + 1 + 2 = 9. Input : n = "12345" Output : 7
Fuente: Conjunto de entrevistas de Morgan Stanley 20
La idea es calcular previamente las luces LED necesarias para mostrar un número determinado. Ahora itere el número y siga agregando los cambios. Para la implementación, se utiliza un concepto básico de hashing de strings.
A continuación se muestra la implementación del problema anterior.
C++
// CPP program to count number of on offs to // display digits of a number. #include<bits/stdc++.h> using namespace std; int countOnOff(string n) { // store the led lights required to display // a particular number. int Led[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 }; int len = n.length(); // compute the change in led and keep // on adding the change int sum = Led[n[0] - '0']; for (int i = 1; i < len; i++) { sum = sum + abs(Led[n[i] - '0'] - Led[n[i - 1] - '0']); } return sum; } // Driver code int main() { string n = "082"; cout << countOnOff(n); return 0; }
Java
// Java program to count number of on offs to // display digits of a number. import java.io.*; class GFG { static int countOnOff(String n) { // store the led lights required to display // a particular number. int Led[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 }; int len = n.length(); // compute the change in led and keep // on adding the change int sum = Led[n.charAt(0) - '0']; for (int i = 1; i < len; i++) { sum = sum + Math.abs(Led[n.charAt(i) - '0'] - Led[n.charAt(i - 1) - '0']); } return sum; } // Driver code public static void main(String args[]) { String n = "082"; System.out.println( countOnOff(n) ); } }
Python 3
# Python3 program to count number of on offs to # display digits of a number. def countOnOff(n): # store the led lights required to display # a particular number. Led = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 ] leng = len(n) # compute the change in led and keep # on adding the change sum = Led[int(n[0]) - int('0')] for i in range(1,leng): sum = (sum + abs(Led[int(n[i]) - int('0')] - Led[int(n[i - 1]) - int('0')])) return sum #Driver code if __name__=='__main__': n = "082" print(countOnOff(n)) # this code is contributed by # ash264
C#
// C# program to count number of on // offs to display digits of a number. using System; class GFG { public static int countOnOff(string n) { // store the led lights required // to display a particular number. int[] Led = new int[] {6, 2, 5, 5, 4, 5, 6, 3, 7, 5}; int len = n.Length; // compute the change in led and // keep on adding the change int sum = Led[n[0] - '0']; for (int i = 1; i < len; i++) { sum = sum + Math.Abs(Led[n[i] - '0'] - Led[n[i - 1] - '0']); } return sum; } // Driver code public static void Main(string[] args) { string n = "082"; Console.WriteLine(countOnOff(n)); } } // This code is contributed by Shrikant13
PHP
<?php // PHP program to count number // of on offs to display digits // of a number. function countOnOff($n) { // store the led lights required // to display a particular number. $Led = array(6, 2, 5, 5, 4, 5, 6, 3, 7, 5 ); $len = strlen($n); // compute the change in led // and keep on adding the change $sum = $Led[$n[0] - '0']; for ($i = 1; $i < $len; $i++) { $sum = $sum + abs($Led[$n[$i] - '0'] - $Led[$n[$i - 1] - '0']); } return $sum; } // Driver code $n = "082"; echo countOnOff($n); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // javascript program to count number of on offs to // display digits of a number. function countOnOff( n) { // store the led lights required to display // a particular number. var Led = [ 6, 2, 5, 5, 4, 5, 6, 3, 7, 5 ]; var len = n.length; // compute the change in led and keep // on adding the change var sum = Led[n.charAt(0) - '0']; for (i = 1; i < len; i++) { sum = sum + Math.abs(Led[n.charAt(i) - '0'] - Led[n.charAt(i - 1) - '0']); } return sum; } // Driver code n = "082"; document.write( countOnOff(n) ); // This code is contributed by 29AjayKumar </script>
9
Publicación traducida automáticamente
Artículo escrito por Kushdeep_Mittal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA