检查数组的所有元素是否都是回文
给定一个包含N个元素的数组arr[] 。任务是检查数组是否是PalinArray ,即数组的所有元素是否都是回文。
例子:
Input: arr[] = {121, 131, 20}
Output: Array is not a PalinArray
For the given array, element 20 is not a palindrome. Thus the array is not a PalinArray.
Input: arr[] = {111, 121, 222, 333, 444}
Output: Array is a PalinArray
For the given array, all the elements of the array are palindromes. Thus the array is a PalinArray.
方法:
- 遍历给定数组的所有元素并检查每个元素是否是回文。
- 如果是,则 print Array 是一个 PalinArray。
- 否则,print Array 不是 PalinArray。
下面是上述方法的实现:
C++
// CPP implementation to check
// if an array is PalinArray or not
#include
using namespace std;
// Function to check if palindrome or not
bool isPalindrome(int N)
{
string str = "" + N;
int len = str.length();
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - 1 - i])
return false;
}
return true;
}
// Function to check
// if an array is PalinArray or not
bool isPalinArray(int arr[] , int n)
{
// Traversing each element of the array
// and check if it is palindrome or not
for (int i = 0; i < n; i++) {
bool ans = isPalindrome(arr[i]);
if (ans == false)
return false;
}
return true;
}
// Driver code
int main()
{
int arr[] = { 121, 131, 20 };
// length of array
int n = sizeof(arr)/sizeof(arr[0]);
bool res = isPalinArray(arr, n);
if (res == true)
cout<<"Array is a PalinArray";
else
cout<<"Array is not a PalinArray";
}
// This code is contributed by
// Surendra_Gangwar
Java
// Java implementation to check
// if an array is PalinArray or not
class GFG {
// Function to check if palindrome or not
static boolean isPalindrome(int N)
{
String str = "" + N;
int len = str.length();
for (int i = 0; i < len / 2; i++) {
if (str.charAt(i) != str.charAt(len - 1 - i))
return false;
}
return true;
}
// Function to check
// if an array is PalinArray or not
static boolean isPalinArray(int[] arr, int n)
{
// Traversing each element of the array
// and check if it is palindrome or not
for (int i = 0; i < n; i++) {
boolean ans = isPalindrome(arr[i]);
if (ans == false)
return false;
}
return true;
}
// Driver code
public static void main(String args[])
{
int[] arr = { 121, 131, 20 };
// length of array
int n = arr.length;
boolean res = isPalinArray(arr, n);
if (res == true)
System.out.println("Array is a PalinArray");
else
System.out.println("Array is not a PalinArray");
}
}
Python3
# Python3 implementation to check
# if an array is PalinArray or not
# Function to check if palindrome or not
def isPalindrome(N):
str1 = "" + str(N)
len1 = len(str1)
for i in range(int(len1 / 2)):
if (str1[i] != str1[len1 - 1 - i]):
return False
return True
# Function to check
# if an array is PalinArray or not
def isPalinArray(arr, n):
# Traversing each element of the array
# and check if it is palindrome or not
for i in range(n):
ans = isPalindrome(arr[i])
if (ans == False):
return False
return True
# Driver code
if __name__ == '__main__':
arr = [ 121, 131, 20 ]
# length of array
n = len(arr)
res = isPalinArray(arr, n)
if (res == True):
print("Array is a PalinArray")
else:
print("Array is not a PalinArray")
# This code is contributed by PrinciRaj1992
C#
// C# implementation to check
// if an array is PalinArray or not
using System;
class GFG
{
// Function to check if palindrome or not
static bool isPalindrome(int N)
{
string str = "" + N;
int len = str.Length;
for (int i = 0; i < len / 2; i++)
{
if (str[i] != str[len - 1 - i ])
return false;
}
return true;
}
// Function to check if an array is
// PalinArray or not
static bool isPalinArray(int[] arr, int n)
{
// Traversing each element of the array
// and check if it is palindrome or not
for (int i = 0; i < n; i++)
{
bool ans = isPalindrome(arr[i]);
if (ans == false)
return false;
}
return true;
}
// Driver code
public static void Main()
{
int[] arr = { 121, 131, 20 };
// length of array
int n = arr.Length;
bool res = isPalinArray(arr, n);
if (res == true)
Console.WriteLine("Array is a PalinArray");
else
Console.WriteLine("Array is not a PalinArray");
}
}
// This code is contributed by Ryuga
PHP
Javascript
输出:
Array is not a PalinArray