📜  数组中所有回文数的总和

📅  最后修改于: 2021-04-27 06:17:58             🧑  作者: Mango

给定N个正整数的数组arr []。任务是找到阵列中所有回文数的总和。打印总和。

回文数是一个数字,当反转时,该数字等于初始数字。例如:121是回文(reverse(121)= 121),123不是回文(reverse(123)= 321)。

注意:计算总和时,考虑长度大于1的回文数。

例子:

Input : arr[] ={12, 313, 11, 44, 9, 1} 
Output : 368

Input : arr[] = {12, 11, 121}
Output : 132

方法:想法是实现一个反向功能,该函数将数字从右向左颠倒。实现一个检查回文数的函数,最后遍历数组并计算所有回文元素的总和。

下面是上述方法的实现:

C++
// C++ program to calculate the sum of all
// palindromic numbers in array
#include
using namespace std;
  
// Function to reverse a number n
int reverse(int n)
{
    int d = 0, s = 0;
  
    while (n > 0) 
    {
        d = n % 10;
        s = s * 10 + d;
        n = n / 10;
    }
  
    return s;
}
  
// Function to check if a number n is
// palindrome
bool isPalin(int n)
{
    // If n is equal to the reverse of n
    // it is a palindrome
    return n == reverse(n);
}
  
// Function to calculate sum of all array
// elements which are palindrome
int sumOfArray(int arr[], int n)
{
    int s = 0;
  
    for (int i = 0; i < n; i++) 
    {
        if ((arr[i] > 10) && isPalin(arr[i])) 
        {
  
            // summation of all palindrome numbers
            // present in array
            s += arr[i];
        }
    }
    return s;
}
  
// Driver Code
int main()
{
    int n = 6;
  
    int arr[] = { 12, 313, 11, 44, 9, 1 };
  
    cout << sumOfArray(arr, n);
    return 0;
}
  
// This code is contributed by mits


Java
// Java program to calculate the sum of all
// palindromic numbers in array
  
class GFG {
  
    // Function to reverse a number n
    static int reverse(int n)
    {
        int d = 0, s = 0;
  
        while (n > 0) {
            d = n % 10;
            s = s * 10 + d;
            n = n / 10;
        }
  
        return s;
    }
  
    // Function to check if a number n is
    // palindrome
    static boolean isPalin(int n)
    {
        // If n is equal to the reverse of n
        // it is a palindrome
        return n == reverse(n);
    }
  
    // Function to calculate sum of all array
    // elements which are palindrome
    static int sumOfArray(int[] arr, int n)
    {
        int s = 0;
  
        for (int i = 0; i < n; i++) {
            if ((arr[i] > 10) && isPalin(arr[i])) {
  
                // summation of all palindrome numbers
                // present in array
                s += arr[i];
            }
        }
  
        return s;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int n = 6;
  
        int[] arr = { 12, 313, 11, 44, 9, 1 };
  
        System.out.println(sumOfArray(arr, n));
    }
}


C#
// C# program to calculate the sum of all 
// palindromic numbers in array 
using System;
  
class GFG 
{ 
  
    // Function to reverse a number n 
    static int reverse(int n) 
    { 
        int d = 0, s = 0; 
  
        while (n > 0)
        { 
            d = n % 10; 
            s = s * 10 + d; 
            n = n / 10; 
        } 
  
        return s; 
    } 
  
    // Function to check if a number n is 
    // palindrome 
    static bool isPalin(int n) 
    { 
        // If n is equal to the reverse of n 
        // it is a palindrome 
        return n == reverse(n); 
    } 
  
    // Function to calculate sum of all array 
    // elements which are palindrome 
    static int sumOfArray(int[] arr, int n) 
    { 
        int s = 0; 
  
        for (int i = 0; i < n; i++)
        { 
            if ((arr[i] > 10) && isPalin(arr[i]))
            { 
  
                // summation of all palindrome numbers 
                // present in array 
                s += arr[i]; 
            } 
        } 
  
        return s; 
    } 
  
    // Driver Code 
    public static void Main(String[] args) 
    { 
        int n = 6; 
  
        int[] arr = { 12, 313, 11, 44, 9, 1 }; 
  
        Console.WriteLine(sumOfArray(arr, n)); 
    } 
} 
  
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 program to calculate the sum of all
# palindromic numbers in array
  
# Function to reverse a number n
def reverse(n) :
      
    d = 0; s = 0;
  
    while (n > 0) :
  
        d = n % 10;
        s = s * 10 + d;
        n = n // 10;
  
    return s;
      
  
# Function to check if a number n is
# palindrome
def isPalin(n) :
  
    # If n is equal to the reverse of n
    # it is a palindrome
    return n == reverse(n);
  
  
# Function to calculate sum of all array
# elements which are palindrome
def sumOfArray(arr, n) :
    s = 0;
      
    for i in range(n) :
        if ((arr[i] > 10) and isPalin(arr[i])) :
          
            # summation of all palindrome numbers
            # present in array
            s += arr[i];
              
    return s;
   
  
# Driver Code
if __name__ == "__main__" :
  
    n = 6;
  
    arr = [ 12, 313, 11, 44, 9, 1 ];
  
    print(sumOfArray(arr, n));
     
    # This code is contributed by AnkitRai01


输出:
368