Cuente los dígitos presentes en cada elemento de una Array dada

Dada una array arr[][] de dimensiones M * N , la tarea es contar el número de dígitos de cada elemento presente en la array dada.

Ejemplos:

Entrada: arr[][] = { { 27, 173, 5 }, { 21, 6, 624 }, { 5, 321, 49 } }
Salida: 
2 3 1
2 1 3
1 3 2

Entrada: arr[][] = { {11, 12, 33 }, { 64, 57, 61 }, { 74, 88, 39 } }
Salida: 
2 2 2
2 2 2
2 2 2

Enfoque: La idea para resolver este problema es atravesar la array dada y para cada índice de la array, contar el número de dígitos del número presente en ese índice usando la siguiente expresión:

El número de dígitos presentes en cualquier valor X viene dado por floor(log10(X))+1 .

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

C++

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
const int M = 3;
const int N = 3;
 
// Function to count the number of digits
// in each element of the given matrix
void countDigit(int arr[M][N])
{
 
    // Traverse each row of arr[][]
    for (int i = 0; i < M; i++) {
 
        // Traverse each column of arr[][]
        for (int j = 0; j < N; j++) {
 
            // Store the current matrix element
            int X = arr[i][j];
 
            // Count the number of digits
            int d = floor(log10(X) * 1.0) + 1;
 
            // Print the result
            cout << d << " ";
        }
 
        cout << endl;
    }
}
 
// Driver Code
int main()
{
    // Given matrix
    int arr[][3] = { { 27, 173, 5 },
                     { 21, 6, 624 },
                     { 5, 321, 49 } };
 
    countDigit(arr);
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
class GFG
{
 
static int M = 3;
static int N = 3;
 
// Function to count the number of digits
// in each element of the given matrix
static void countDigit(int arr[][])
{
 
    // Traverse each row of arr[][]
    for (int i = 0; i < M; i++)
    {
 
        // Traverse each column of arr[][]
        for (int j = 0; j < N; j++)
        {
 
            // Store the current matrix element
            int X = arr[i][j];
 
            // Count the number of digits
            int d = (int) (Math.floor(Math.log10(X) * 1.0) + 1);
 
            // Print the result
            System.out.print(d+ " ");
        }
        System.out.println();
    }
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given matrix
    int arr[][] = { { 27, 173, 5 },
                     { 21, 6, 624 },
                     { 5, 321, 49 } };
    countDigit(arr);
}
}
 
// This code is contributed by Princi Singh

Python3

# Python3 program for the above approach
from math import floor, log10
 
M = 3
N = 3
 
# Function to count the number of digits
# in each element of the given matrix
def countDigit(arr):
     
    # Traverse each row of arr[][]
    for i in range(M):
 
        # Traverse each column of arr[][]
        for j in range(N):
 
            # Store the current matrix element
            X = arr[i][j]
 
            # Count the number of digits
            d = floor(log10(X) * 1.0) + 1
 
            # Print the result
            print(d, end = " ")
 
        print()
 
# Driver Code
if __name__ == '__main__':
     
    # Given matrix
    arr = [ [ 27, 173, 5 ],
            [ 21, 6, 624 ],
            [ 5, 321, 49 ] ]
 
    countDigit(arr)
 
# This code is contributed by mohit kumar 29

C#

// C# program to implement
// the above approach 
using System;
class GFG
{
      
static int M = 3;
static int N = 3;
  
// Function to count the number of digits
// in each element of the given matrix
static void countDigit(int[,] arr)
{
  
    // Traverse each row of arr[][]
    for (int i = 0; i < M; i++)
    {
  
        // Traverse each column of arr[][]
        for (int j = 0; j < N; j++)
        {
  
            // Store the current matrix element
            int X = arr[i, j];
  
            // Count the number of digits
            int d = (int) (Math.Floor(Math.Log10(X) * 1.0) + 1);
  
            // Print the result
            Console.Write(d + " ");
        }
        Console.WriteLine();
    }
}
  
// Driver Code
public static void Main()
{
   
    // Given matrix
    int[,] arr = { { 27, 173, 5 },
                     { 21, 6, 624 },
                     { 5, 321, 49 } };
    countDigit(arr);
}
}
 
// This code is contributed by susmitakundugoaldanga

Javascript

<script>
 
// JavaScript program to implement
// the above approach
 
let M = 3;
let N = 3;
  
// Function to count the number of digits
// in each element of the given matrix
function countDigit(arr)
{
  
    // Traverse each row of arr[][]
    for (let i = 0; i < M; i++)
    {
  
        // Traverse each column of arr[][]
        for (let j = 0; j < N; j++)
        {
  
            // Store the current matrix element
            let X = arr[i][j];
  
            // Count the number of digits
            let d =  (Math.floor(Math.log10(X) * 1.0) + 1);
  
            // Print the result
            document.write(d+ " ");
        }
        document.write("<br/>");
    }
}
 
      
// Driver code
         
    // Given matrix
    let arr = [[ 27, 173, 5 ],
                     [ 21, 6, 624 ],
                     [ 5, 321, 49 ]];
    countDigit(arr);
 
</script>
Producción: 

2 3 1 
2 1 3 
1 3 2

 

Complejidad de Tiempo: O(M * N)
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

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