Dado un rango [L, R] , la tarea es encontrar el conteo de números del rango cuyo último dígito es 2 , 3 o 9 .
Ejemplos:
Entrada: L = 1, R = 3
Salida: 2
2 y 3 son los únicos números válidos.
Entrada: L = 11, R = 33
Salida: 8
Enfoque: Inicialice un conteo de contador = 0 y ejecute un ciclo para cada elemento del rango, si el elemento actual termina con cualquiera de los dígitos dados, incremente el conteo.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the count // of the required numbers int countNums(int l, int r) { int cnt = 0; for (int i = l; i <= r; i++) { // Last digit of the current number int lastDigit = (i % 10); // If the last digit is equal to // any of the given digits if ((lastDigit % 10) == 2 || (lastDigit % 10) == 3 || (lastDigit % 10) == 9) { cnt++; } } return cnt; } // Driver code int main() { int l = 11, r = 33; cout << countNums(l, r) ; } // This code is contributed by AnkitRai01
Java
// Java implementation of the approach class GFG { // Function to return the count // of the required numbers static int countNums(int l, int r) { int cnt = 0; for (int i = l; i <= r; i++) { // Last digit of the current number int lastDigit = (i % 10); // If the last digit is equal to // any of the given digits if ((lastDigit % 10) == 2 || (lastDigit % 10) == 3 || (lastDigit % 10) == 9) { cnt++; } } return cnt; } // Driver code public static void main(String[] args) { int l = 11, r = 33; System.out.print(countNums(l, r)); } }
Python3
# Python3 implementation of the approach # Function to return the count # of the required numbers def countNums(l, r) : cnt = 0; for i in range(l, r + 1) : # Last digit of the current number lastDigit = (i % 10); # If the last digit is equal to # any of the given digits if ((lastDigit % 10) == 2 or (lastDigit % 10) == 3 or (lastDigit % 10) == 9) : cnt += 1; return cnt; # Driver code if __name__ == "__main__" : l = 11; r = 33; print(countNums(l, r)) ; # This code is contributed by AnkitRai01
C#
// C# implementation of the approach using System; class GFG { // Function to return the count // of the required numbers static int countNums(int l, int r) { int cnt = 0; for (int i = l; i <= r; i++) { // Last digit of the current number int lastDigit = (i % 10); // If the last digit is equal to // any of the given digits if ((lastDigit % 10) == 2 || (lastDigit % 10) == 3 || (lastDigit % 10) == 9) { cnt++; } } return cnt; } // Driver code public static void Main() { int l = 11, r = 33; Console.Write(countNums(l, r)); } } // This code is contributed by AnkitRai01
Javascript
<script> // JavaScript implementation of the approach // Function to return the count // of the required numbers function countNums(l, r) { let cnt = 0; for (let i = l; i <= r; i++) { // Last digit of the current number let lastDigit = (i % 10); // If the last digit is equal to // any of the given digits if ((lastDigit % 10) == 2 || (lastDigit % 10) == 3 || (lastDigit % 10) == 9) { cnt++; } } return cnt; } let l = 11, r = 33; document.write(countNums(l, r)); </script>
Producción:
8
Complejidad de tiempo: O(r)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por coodieaniket y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA