Cuente números con N dígitos que consisten en un número par de 0

Dado un número N. La tarea es encontrar el conteo de números que tienen N dígitos y un número par de ceros.
Nota: El número puede tener ceros anteriores.
Ejemplos
 

Input: N = 2
Output: Count = 81 
Total 2 digit numbers are 99 considering 1 as 01.
2 digit numbers are 01, 02, 03, 04, 05.... 99
Numbers with odd 0's are 01, 02, 03, 04, 05, 06, 07, 08, 09
10, 20, 30, 40, 50, 70, 80, 90 i.e. 18
The rest of the numbers between 01 and 99 will 
do not have any zeroes and zero is also an even number.
So, numbers with even 0's are 99 - 18 = 81.

Input: N = 3
Output: Count = 755

Enfoque: la idea es encontrar los números de conteo con N dígitos que consisten en un número impar de 0 y restarlo del número total con N dígitos para obtener el número con 0 pares.
 

C++

// C++ program to count numbers with N digits
// which consists of odd number of 0's
#include <bits/stdc++.h>
using namespace std;
 
// Function to count Numbers with N digits
// which consists of odd number of 0's
int countNumbers(int N)
{
    return (pow(10, N) - 1) - (pow(10, N) - pow(8, N)) / 2;
}
 
// Driver code
int main()
{
    int n = 2;
 
    cout << countNumbers(n) << endl;
 
    return 0;
}

Java

// Java program to count numbers
// with N digits which consists
// of odd number of 0's
import java.lang.*;
import java.util.*;
 
class GFG
{
     
// Function to count Numbers with
// N digits which consists of odd
// number of 0's
static double countNumbers(int N)
{
    return (Math.pow(10, N) - 1) -
           (Math.pow(10, N) -
            Math.pow(8, N)) / 2;
}
 
// Driver code
static public void main (String args[])
{
    int n = 2;
    System.out.println(countNumbers(n));
}
}
 
// This code si contributed
// by Akanksha Rai

Python3

# Python3  program to count numbers with N digits
#  which consists of odd number of 0's
 
# Function to count Numbers with N digits
#  which consists of odd number of 0's
def countNumber(n):
 
    return (pow(10,n)-1)- (pow(10,n)-pow(8,n))//2
 
 
# Driver code
n = 2
print(countNumber(n))
 
# This code is contributed by Shrikant13

C#

// C# program to count numbers
// with N digits which consists
// of odd number of 0's
using System;
 
class GFG
{
     
// Function to count Numbers with
// N digits which consists of odd
// number of 0's
static double countNumbers(int N)
{
    return (Math.Pow(10, N) - 1) -
           (Math.Pow(10, N) -
            Math.Pow(8, N)) / 2;
}
 
// Driver code
static public void Main ()
{
    int n = 2;
    Console.WriteLine(countNumbers(n));
}
}
 
// This code si contributed by ajit

PHP

<?php
// PHP program to count numbers with N digits
// which consists of odd number of 0's
 
// Function to count Numbers with N digits
// which consists of odd number of 0's
function countNumbers($N)
{
    return (pow(10, $N) - 1) -
           (pow(10, $N) - pow(8, $N)) / 2;
}
 
// Driver code
$n = 2;
echo countNumbers($n),"\n";
 
// This code is contributed by akt_mit
?>

Javascript

<script>
 
// Javascript program to count numbers with N digits
// which consists of odd number of 0's
 
// Function to count Numbers with N digits
// which consists of odd number of 0's
function countNumbers(N)
{
    return (Math.pow(10, N) - 1) - (Math.pow(10, N) - Math.pow(8, N)) / 2;
}
 
// Driver code
var n = 2;
document.write( countNumbers(n));
 
</script>
Producción: 

81

 

Complejidad temporal: O(logN), ya que la función pow tarda (log N) veces en encontrar la potencia en base N.

Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.

Nota : la respuesta puede ser muy grande, por lo que para N mayor que 9, use exponenciación modular.
 

Publicación traducida automáticamente

Artículo escrito por Shivam.Pradhan 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 *