Contar número hexadecimal

Dado un rango [L, R] . La tarea es encontrar el número total de alfabetos hexadecimales que se requieren para escribir cada número en el rango. 
Los alfabetos hexadecimales son los alfabetos en el rango [A, F] que se requieren para representar números decimales del rango [10, 15]
Ejemplos: 
 

Entrada: L = 10, R = 15 
Salida:
Todos los números del 10 al 15 contienen un alfabeto hexadecimal.
Entrada: L = 15, R = 16 
Salida:
15 y 16 se representan en hexadecimal como F y 10 respectivamente. 
 

Acercarse: 
 

  1. En primer lugar, comprueba si num ≥ 10 y num ≤ 15 . En caso afirmativo, incremente el conteo ya que los números decimales del 10 al 15 contienen un alfabeto hexadecimal.
  2. Si num > 15 , actualice el número como num = num % 16 . Si es mayor que 10, incremente el conteo.
  3. Repita el segundo paso hasta que el número (para cada número) sea mayor que 0.

A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that will count
// total hexadecimal alphabet
int countHexadecimal(int L, int R)
{
    int count = 0;
    for (int i = L; i <= R; i++) {
 
        // All the numbers from 10 to 15
        // contain a hexadecimal alphabet
        if (i >= 10 && i <= 15)
            count++;
 
        // If i > 15 then perform mod by 16 repeatedly
        // till the number is > 0
        // If number % 16 > 10 then increase count
        else if (i > 15) {
            int k = i;
            while (k != 0) {
                if (k % 16 >= 10)
                    count++;
                k = k / 16;
            }
        }
    }
 
    return count;
}
 
// Driver code
int main()
{
    int L = 5, R = 100;
    cout << countHexadecimal(L, R);
 
    return 0;
}

Java

// Java implementation of the approach
class GFG
{
     
// Function that will count
// total hexadecimal alphabet
static int countHexadecimal(int L, int R)
{
    int count = 0;
    for (int i = L; i <= R; i++)
    {
 
        // All the numbers from 10 to 15
        // contain a hexadecimal alphabet
        if (i >= 10 && i <= 15)
            count++;
 
        // If i > 15 then perform mod by 16
        // repeatedly till the number is > 0
        // If number % 16 > 10 then increase count
        else if (i > 15)
        {
            int k = i;
            while (k != 0)
            {
                if (k % 16 >= 10)
                    count++;
                k = k / 16;
            }
        }
    }
 
    return count;
}
 
// Driver code
public static void main(String args[])
{
    int L = 5, R = 100;
    System.out.print(countHexadecimal(L, R));
}
}
 
// This code is contributed
// by Akanksha Rai

Python3

# Python3 implementation of the approach
 
# Function that will count
# total hexadecimal alphabet
def countHexadecimal(L, R) :
    count = 0;
    for i in range(L, R + 1) :
 
        # All the numbers from 10 to 15
        # contain a hexadecimal alphabet
        if (i >= 10 and i <= 15) :
            count += 1;
 
        # If i > 15 then perform mod by 16
        # repeatedly till the number is > 0
        # If number % 16 > 10 then
        # increase count
        elif (i > 15) :
            k = i;
            while (k != 0) :
                if (k % 16 >= 10) :
                    count += 1;
                k = k // 16;
 
    return count;
 
# Driver code
if __name__ == "__main__" :
    L = 5; R = 100;
     
    print(countHexadecimal(L, R));
 
# This code is contributed by Ryuga

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
// Function that will count
// total hexadecimal alphabet
static int countHexadecimal(int L, int R)
{
    int count = 0;
    for (int i = L; i <= R; i++)
    {
 
        // All the numbers from 10 to 15
        // contain a hexadecimal alphabet
        if (i >= 10 && i <= 15)
            count++;
 
        // If i > 15 then perform mod by 16 repeatedly
        // till the number is > 0
        // If number % 16 > 10 then increase count
        else if (i > 15)
        {
            int k = i;
            while (k != 0)
            {
                if (k % 16 >= 10)
                    count++;
                k = k / 16;
            }
        }
    }
 
    return count;
}
 
// Driver code
public static void Main()
{
    int L = 5, R = 100;
    Console.Write(countHexadecimal(L, R));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP implementation of the approach
 
// Function that will count
// total hexadecimal alphabet
function countHexadecimal($L, $R)
{
    $count = 0;
    for ($i = $L; $i <= $R; $i++)
    {
 
        // All the numbers from 10 to 15
        // contain a hexadecimal alphabet
        if ($i >= 10 && $i <= 15)
            $count++;
 
        // If i > 15 then perform mod by 16
        // repeatedly till the number is > 0
        // If number % 16 > 10 then increase count
        else if ($i > 15)
        {
            $k = $i;
            while ($k != 0)
            {
                if ($k % 16 >= 10)
                    $count++;
                $k = $k / 16;
            }
        }
    }
 
    return $count;
}
 
// Driver code
$L = 5;
$R = 100;
echo countHexadecimal($L, $R);
 
// This code is contributed by Ita_c
?>

Javascript

<script>
 
// Javascript implementation of the approach
 
// Function that will count
// total hexadecimal alphabet
function countHexadecimal(L, R)
{
    var count = 0;
    for (var i = L; i <= R; i++) {
 
        // All the numbers from 10 to 15
        // contain a hexadecimal alphabet
        if (i >= 10 && i <= 15)
            count++;
 
        // If i > 15 then perform mod by 16 repeatedly
        // till the number is > 0
        // If number % 16 > 10 then increase count
        else if (i > 15) {
            var k = i;
            while (k != 0) {
                if (k % 16 >= 10)
                    count++;
                k = k / 16;
            }
        }
    }
 
    return count;
}
 
// Driver code
var L = 5, R = 100;
document.write( countHexadecimal(L, R));
 
// This code is contributed by rutvik_56.
</script>
Producción: 

36

 

Publicación traducida automáticamente

Artículo escrito por Naman_Garg 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 *