📌  相关文章
📜  计算具有相同的第一位和最后一位数字的非回文数组元素

📅  最后修改于: 2021-05-17 06:21:10             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是打印出现在给定数组中的第一个和最后一个数字相同的非回文数的计数。

例子:

方法:可以通过检查每个数组元素(无论是否是回文)来解决该问题。请按照以下步骤解决问题。

  1. 遍历数组arr []
  2. 对于每个数组元素arr [i] ,检查它是否是回文。
  3. 对于发现不是回文的每个数组元素,请在数字反转之前和之后提取最后一位数字。提取后,检查数字是否相等。
  4. 如果发现是真的,则增加计数。
  5. 最后,打印这些数字的计数。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
  
#include 
using namespace std;
  
// Function to reverse a number
int revNum(int N)
{
    // Store the reverse of N
    int x = 0;
    while (N) {
        x = x * 10 + N % 10;
        N = N / 10;
    }
  
    // Return reverse of N
    return x;
}
  
// Function to get the count of non-palindromic
// numbers having same first and last digit
int ctNonPalin(int arr[], int N)
{
  
    // Store the required count
    int Res = 0;
  
    // Traverse the array
    for (int i = 0; i < N; i++) {
  
        // Store reverse of arr[i]
        int x = revNum(arr[i]);
  
        // Check for palindrome
        if (x == arr[i]) {
            continue;
        }
  
        // IF non-palindromic
        else {
            // Check if first and last
            // digits are equal
            Res += (arr[i] % 10 == N % 10);
        }
    }
    return Res;
}
  
// Driver Code
int main()
{
    int arr[] = { 121, 134, 2342, 4514 };
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << ctNonPalin(arr, N);
}


Java
// Java program to implement 
// the above approach 
import java.io.*; 
  
class GFG{ 
  
// Function to reverse a number
static int revNum(int N)
{
      
    // Store the reverse of N
    int x = 0;
      
    while (N != 0)
    {
        x = x * 10 + N % 10;
        N = N / 10;
    }
  
    // Return reverse of N
    return x;
}
  
// Function to get the count of non-palindromic
// numbers having same first and last digit
static int ctNonPalin(int arr[], int N)
{
  
    // Store the required count
    int Res = 0;
  
    // Traverse the array
    for(int i = 0; i < N; i++) 
    {
          
        // Store reverse of arr[i]
        int x = revNum(arr[i]);
  
        // Check for palindrome
        if (x == arr[i])
        {
            continue;
        }
  
        // IF non-palindromic
        else
        {
              
            // Check if first and last
            // digits are equal
            if(arr[i] % 10 == x % 10)
                Res += 1;
        }
    }
    return Res;
}
  
// Driver code 
public static void main (String[] args) 
{ 
    int arr[] = { 121, 134, 2342, 4514 };
    int N = arr.length;
      
    System.out.println(ctNonPalin(arr, N));
} 
} 
  
// This code is contributed by jana_sayantan


Python3
# Python3 program to implement
# the above approach
  
# Function to reverse a number
def revNum(N):
  
    # Store the reverse of N
    x = 0
    while (N):
        x = x * 10 + N % 10
        N = N // 10
  
    # Return reverse of N
    return x
  
# Function to get the count of non-palindromic
# numbers having same first and last digit
def ctNonPalin(arr, N):
  
    # Store the required count
    Res = 0
  
    # Traverse the array
    for i in range(N):
  
        # Store reverse of arr[i]
        x = revNum(arr[i])
  
        # Check for palindrome
        if (x == arr[i]):
            continue
  
        # IF non-palindromic
        else:
              
            # Check if first and last
            # digits are equal
            Res += (arr[i] % 10 == N % 10)
  
    return Res
  
# Driver Code
if __name__ == '__main__':
  
    arr = [ 121, 134, 2342, 4514 ]
    N = len(arr)
      
    print(ctNonPalin(arr, N))
  
# This code is contributed by mohit kumar 29


C#
// C# program to implement 
// the above approach 
using System;
  
class GFG{ 
  
// Function to reverse a number
static int revNum(int N)
{
      
    // Store the reverse of N
    int x = 0;
      
    while (N != 0)
    {
        x = x * 10 + N % 10;
        N = N / 10;
    }
  
    // Return reverse of N
    return x;
}
  
// Function to get the count of non-palindromic
// numbers having same first and last digit
static int ctNonPalin(int[] arr, int N)
{
  
    // Store the required count
    int Res = 0;
  
    // Traverse the array
    for(int i = 0; i < N; i++) 
    {
          
        // Store reverse of arr[i]
        int x = revNum(arr[i]);
  
        // Check for palindrome
        if (x == arr[i])
        {
            continue;
        }
  
        // IF non-palindromic
        else
        {
              
            // Check if first and last
            // digits are equal
            if(arr[i] % 10 == x % 10)
                Res += 1;
        }
    }
    return Res;
}
  
// Driver code 
public static void Main () 
{ 
    int[] arr = new int[]{ 121, 134, 2342, 4514 };
    int N = arr.Length;
      
    Console.WriteLine(ctNonPalin(arr, N));
} 
}
  
// This code is contributed by sanjoy_62


输出:
2

时间复杂度: O(N * D)其中D是数组中最大数字的长度。
辅助空间: O(D)