📜  计算数组中回文数的位数

📅  最后修改于: 2021-04-26 07:32:42             🧑  作者: Mango

给定一个具有N个整数的数组arr [] 。任务是计算数组中存在的所有回文数的所有数字。

例子:

方法:对于数组的每个元素,如果它是一个数字,则在数字的答案中加1,否则检查数字是否是回文。如果是,则找到其位数,并将其添加到答案中。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
// Function to return the reverse of n
int reverse(int n)
{
    int rev = 0;
    while (n > 0) 
    {
        int d = n % 10;
        rev = rev * 10 + d;
        n = n / 10;
    }
    return rev;
}
  
// Function that returns true
// if n is a palindrome
bool isPalin(int n)
{
    return (n == reverse(n));
}
  
// Function to return the
// count of digits of n
int countDigits(int n)
{
    int c = 0;
    while (n > 0) 
    {
        n = n / 10;
        c++;
    }
    return c;
}
  
// Function to return the count of digits
// in all the palindromic numbers of arr[]
int countPalinDigits(int arr[], int n)
{
    int s = 0;
    for (int i = 0; i < n; i++) 
    {
  
        // If arr[i] is a one digit number
        // or it is a palindrome
        if (arr[i] < 10 || isPalin(arr[i])) 
        {
            s += countDigits(arr[i]);
        }
    }
    return s;
}
  
// Driver code
int main()
{
    int arr[] = { 121, 56, 434 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << (countPalinDigits(arr, n));
    return 0;
}
  
// This code is contributed by mits


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the reverse of n
    static int reverse(int n)
    {
        int rev = 0;
        while (n > 0) {
            int d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
  
    // Function that returns true
    // if n is a palindrome
    static boolean isPalin(int n)
    {
        return (n == reverse(n));
    }
  
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int c = 0;
        while (n > 0) {
            n = n / 10;
            c++;
        }
        return c;
    }
  
    // Function to return the count of digits
    // in all the palindromic numbers of arr[]
    static int countPalinDigits(int[] arr, int n)
    {
        int s = 0;
        for (int i = 0; i < n; i++) {
  
            // If arr[i] is a one digit number
            // or it is a palindrome
            if (arr[i] < 10 || isPalin(arr[i])) {
                s += countDigits(arr[i]);
            }
        }
        return s;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int[] arr = { 121, 56, 434 };
        int n = arr.length;
        System.out.println(countPalinDigits(arr, n));
    }
}


Python3
# Python3 implementation of the approach 
  
# Function to return the reverse of n 
def reverse(n): 
    rev = 0; 
    while (n > 0):
        d = n % 10; 
        rev = rev * 10 + d; 
        n = n // 10; 
    return rev; 
  
# Function that returns true 
# if n is a palindrome 
def isPalin(n): 
    return (n == reverse(n)); 
  
  
# Function to return the 
# count of digits of n 
def countDigits(n): 
    c = 0; 
    while (n > 0): 
        n = n // 10; 
        c += 1;
    return c; 
  
# Function to return the count of digits 
# in all the palindromic numbers of arr[] 
def countPalinDigits(arr, n): 
    s = 0; 
    for i in range(n):
  
        # If arr[i] is a one digit number 
        # or it is a palindrome 
        if (arr[i] < 10 or isPalin(arr[i])):
            s += countDigits(arr[i]); 
  
    return s; 
  
  
# Driver code 
arr = [ 121, 56, 434 ]; 
n = len(arr); 
print(countPalinDigits(arr, n)); 
  
# This code contributed by Rajput-Ji


C#
// C# implementation of the approach 
using System;
      
class GFG 
{
  
    // Function to return the reverse of n
    static int reverse(int n)
    {
        int rev = 0;
        while (n > 0)
        {
            int d = n % 10;
            rev = rev * 10 + d;
            n = n / 10;
        }
        return rev;
    }
  
    // Function that returns true
    // if n is a palindrome
    static bool isPalin(int n)
    {
        return (n == reverse(n));
    }
  
    // Function to return the
    // count of digits of n
    static int countDigits(int n)
    {
        int c = 0;
        while (n > 0) 
        {
            n = n / 10;
            c++;
        }
        return c;
    }
  
    // Function to return the count of digits
    // in all the palindromic numbers of arr[]
    static int countPalinDigits(int[] arr, int n)
    {
        int s = 0;
        for (int i = 0; i < n; i++)
        {
  
            // If arr[i] is a one digit number
            // or it is a palindrome
            if (arr[i] < 10 || isPalin(arr[i])) 
            {
                s += countDigits(arr[i]);
            }
        }
        return s;
    }
  
    // Driver code
    public static void Main()
    {
        int[] arr = { 121, 56, 434 };
        int n = arr.Length;
        Console.WriteLine(countPalinDigits(arr, n));
    }
}
  
/* This code contributed by PrinciRaj1992 */


Python3
# Function to return the count of digits 
# in all the palindromic numbers of arr[] 
def countPalinDigits(arr): 
   sum = 0
   
   for n in arr:
      n_str = str(n)
      l = len(n_str)
      if n_str[l::-1] == n_str: # if palindrome
         sum += l 
   return sum
  
# Driver code 
arr = [ 121, 56, 434 ]; 
print(countPalinDigits(arr));


输出:
6

较短的Python实现

Python3

# Function to return the count of digits 
# in all the palindromic numbers of arr[] 
def countPalinDigits(arr): 
   sum = 0
   
   for n in arr:
      n_str = str(n)
      l = len(n_str)
      if n_str[l::-1] == n_str: # if palindrome
         sum += l 
   return sum
  
# Driver code 
arr = [ 121, 56, 434 ]; 
print(countPalinDigits(arr)); 
输出:
6