Conteo de números con todos los dígitos iguales en un rango dado

Dados dos números enteros L y R que denotan los valores inicial y final de un rango, la tarea es contar todos los números en ese rango cuyos dígitos sean todos iguales, como 1, 22, 444, 3333, etc.
Ejemplo: 
 

Entrada: L = 12, R = 68 
Salida:
Explicación: 
{ 22, 33, 44, 55, 66} son los números con los mismos dígitos en el rango dado.
Entrada: L = 1, R = 32 
Salida: 11 
 

Enfoque ingenuo: iterar a través de todos los números de L a R y para cada número, verificar si tiene todos sus dígitos iguales. En caso afirmativo, aumente el recuento requerido. Imprime este conteo al final.
Enfoque Eficiente: La idea se basa en el hecho de que los múltiplos (1 a 9) de 1, 11, 111, etc. tienen todos sus dígitos iguales. 
Por ejemplo: 
 

1 times 1 = 1 (All digits are same)
2 times 1 = 2 (All digits are same)
3 times 1 = 3 (All digits are same)
.
.
9 times 1 = 9 (All digits are same)

Similarly
1 times 11 = 11 (All digits are same)
2 times 11 = 22 (All digits are same)
3 times 11 = 33 (All digits are same)
.
.
9 times 11 = 99 (All digits are same)

Same is the case for 111, 1111, etc.

Por lo tanto, los pasos se pueden definir como: 
 

  1. Encuentre la cantidad de dígitos en R. Esto decidirá la longitud de los 1 consecutivos que se crearán, hasta los cuales debemos verificar. 
    Por ejemplo, si R = 100, entonces length(R) = 3. Por lo tanto, solo debemos verificar los múltiplos de 1, 11 y 111. 
     
  2. Para cada longitud de 1s consecutivos de 1 a longitud(R): 
    • Multiplícalos con todos los valores del 2 al 9
    • Compruebe si se encuentra dentro del rango [L, R] o no.
    • Si es así, entonces incremente el conteo de números requeridos.
  3. Imprime el conteo requerido de números.

C++

// C++ program to count the
// total numbers in the range
// L and R which have all the
// digit same
 
#include <bits/stdc++.h>
using namespace std;
 
// Function that count the
// total numbersProgram between L
// and R which have all the
// digit same
int count_same_digit(int L, int R)
{
    int tmp = 0, ans = 0;
 
    // length of R
    int n = log10(R) + 1;
 
    for (int i = 0; i < n; i++) {
 
        // tmp has all digits as 1
        tmp = tmp * 10 + 1;
 
        // For each multiple
        // of tmp in range 1 to 9,
        // check if it present
        // in range [L, R]
        for (int j = 1; j <= 9; j++) {
 
            if (L <= (tmp * j)
                && (tmp * j) <= R) {
 
                // Increment the required count
                ans++;
            }
        }
    }
    return ans;
}
 
// Driver Program
int main()
{
    int L = 12, R = 68;
 
    cout << count_same_digit(L, R)
        << endl;
    return 0;
}

Java

// Java program to count the
// total numbers in the range
// L and R which have all the
// digit same
import java.util.*;
 
class GFG{
 
// Function that count the total
// numbersProgram between L and
// R which have all the digit same
static int count_same_digit(int L, int R)
{
    int tmp = 0, ans = 0;
 
    // Length of R
    int n = (int)Math.log10(R) + 1;
 
    for(int i = 0; i < n; i++)
    {
         
    // tmp has all digits as 1
    tmp = tmp * 10 + 1;
         
    // For each multiple of tmp
    // in range 1 to 9, check if
    // it present in range [L, R]
    for(int j = 1; j <= 9; j++)
    {
        if (L <= (tmp * j) && (tmp * j) <= R)
        {
            // Increment the required count
            ans++;
        }
    }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int L = 12, R = 68;
 
    System.out.println(count_same_digit(L, R));
}
}
 
// This code is contributed by offbeat

Python3

# Python3 program to count the
# total numbers in the range
# L and R which have all the
# digit same
import math
 
# Function that count the
# total numbersProgram between L
# and R which have all the
# digit same
def count_same_digit(L, R):
 
    tmp = 0; ans = 0;
 
    # length of R
    n = int(math.log10(R) + 1);
 
    for i in range(0, n):
 
        # tmp has all digits as 1
        tmp = tmp * 10 + 1;
 
        # For each multiple
        # of tmp in range 1 to 9,
        # check if it present
        # in range [L, R]
        for j in range(1, 9):
 
            if (L <= (tmp * j) and (tmp * j) <= R):
 
                # Increment the required count
                ans += 1;
             
    return ans;
 
# Driver Code
L = 12; R = 68;
 
print(count_same_digit(L, R))
 
# This code is contributed by Nidhi_biet

C#

// C# program to count the
// total numbers in the range
// L and R which have all the
// digit same
using System;
 
class GFG{
 
// Function that count the total
// numbersProgram between L and
// R which have all the digit same
static int count_same_digit(int L, int R)
{
    int tmp = 0, ans = 0;
 
    // Length of R
    int n = (int)Math.Log10(R) + 1;
 
    for(int i = 0; i < n; i++)
    {
         
        // tmp has all digits as 1
        tmp = tmp * 10 + 1;
             
        // For each multiple of tmp
        // in range 1 to 9, check if
        // it present in range [L, R]
        for(int j = 1; j <= 9; j++)
        {
            if (L <= (tmp * j) && (tmp * j) <= R)
            {
                // Increment the required count
                ans++;
            }
        }
    }
    return ans;
}
 
// Driver code
public static void Main()
{
    int L = 12, R = 68;
 
    Console.Write(count_same_digit(L, R));
}
}
 
// This code is contributed by Code_Mech

Javascript

<script>
 
// JavaScript program to count the
// total numbers in the range
// L and R which have all the
// digit same
 
 
 
// Function that count the
// total numbersProgram between L
// and R which have all the
// digit same
function count_same_digit( L,  R)
{
    var tmp = 0, ans = 0;
 
    // length of R
    var n = Math.log10(R) + 1;
 
    for (let i = 0; i < n; i++) {
 
        // tmp has all digits as 1
        tmp = tmp * 10 + 1;
 
        // For each multiple
        // of tmp in range 1 to 9,
        // check if it present
        // in range [L, R]
        for (let j = 1; j <= 9; j++) {
 
            if (L <= (tmp * j)
                && (tmp * j) <= R) {
 
                // Increment the required count
                ans++;
            }
        }
    }
    return ans;
}
 
// Driver Program
    var L = 12, R = 68;
 
    document.write( count_same_digit(L, R));
 
// This code is contributed by ukasp.
</script>

Publicación traducida automáticamente

Artículo escrito por rutvik_56 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *