📜  检查数组是否为回文的程序

📅  最后修改于: 2022-05-13 01:57:51.173000             🧑  作者: Mango

检查数组是否为回文的程序

给定一个数组,任务是确定一个数组是否是回文。
例子:

Input: arr[] = {3, 6, 0, 6, 3}
Output: Palindrome

Input: arr[] = {1, 2, 3, 4, 5}
Output: Not Palindrome

方法:

  • 初始化标志以取消设置int flag = 0
  • 循环数组直到大小 n/2
  • 在循环中检查arr[i]! = arr[ni-1]然后设置标志 = 1中断
  • 循环结束后,如果设置了标志,则打印“非回文” ,否则打印“回文”

以下是上述方法的实现:

C++
// C++ Program to check whether the
// Array is palindrome or not
 
#include 
using namespace std;
 
void palindrome(int arr[], int n)
{
    // Initialise flag to zero.
    int flag = 0;
 
    // Loop till array size n/2.
    for (int i = 0; i <= n / 2 && n != 0; i++) {
 
        // Check if first and last element are different
        // Then set flag to 1.
        if (arr[i] != arr[n - i - 1]) {
            flag = 1;
            break;
        }
    }
 
    // If flag is set then print Not Palindrome
    // else print Palindrome.
    if (flag == 1)
        cout << "Not Palindrome";
    else
        cout << "Palindrome";
}
 
// Driver code.
int main()
{
    int arr[] = { 1, 2, 3, 2, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    palindrome(arr, n);
    return 0;
}


Java
// Java Program to check whether the
// Array is palindromic or not
 
class GfG {
 
    static void palindrome(int arr[], int n)
    {
        // Initialise flag to zero.
        int flag = 0;
 
        // Loop till array size n/2.
        for (int i = 0; i <= n / 2 && n != 0; i++) {
 
            // Check if first and last element are different
            // Then set flag to 1.
            if (arr[i] != arr[n - i - 1]) {
                flag = 1;
                break;
            }
        }
 
        // If flag is set then print Not Palindrome
        // else print Palindrome.
        if (flag == 1)
            System.out.println("Not Palindrome");
        else
            System.out.println("Palindrome");
    }
 
    // Driver code.
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 2, 1 };
        int n = arr.length;
 
        palindrome(arr, n);
    }
}


Python3
# Python3 Program to check whether the
# Array is palindromic or not
def palindrome(arr, n):
 
    # Initialise flag to zero.
    flag = 0;
 
    # Loop till array size n/2.
    i = 0;
    while (i <= n // 2 and n != 0):
 
        # Check if first and last element
        # are different. Then set flag to 1.
        if (arr[i] != arr[n - i - 1]):
            flag = 1;
            break;
        i += 1;
 
    # If flag is set then print Not Palindrome
    # else print Palindrome.
    if (flag == 1):
        print("Not Palindrome");
    else:
        print("Palindrome");
 
# Driver code.
arr = [ 1, 2, 3, 2, 1 ];
n = len(arr);
 
palindrome(arr, n);
 
# This code is contributed
# by chandan_jnu


C#
// C# Program to check whether the
// Array is palindromic or not
class GfG
{
 
    static void palindrome(int []arr, int n)
    {
        // Initialise flag to zero.
        int flag = 0;
 
        // Loop till array size n/2.
        for (int i = 0; i <= n / 2 && n != 0; i++)
        {
 
            // Check if first and last element are different
            // Then set flag to 1.
            if (arr[i] != arr[n - i - 1])
            {
                flag = 1;
                break;
            }
        }
 
        // If flag is set then print Not Palindrome
        // else print Palindrome.
        if (flag == 1)
            System.Console.WriteLine("Not Palindrome");
        else
            System.Console.WriteLine("Palindrome");
    }
 
    // Driver code.
    static void Main()
    {
        int []arr = { 1, 2, 3, 2, 1 };
        int n = arr.Length;
 
        palindrome(arr, n);
    }
}
 
// This code is contributed by chadan_jnu


PHP


Javascript


输出:
Palindrome

相关文章:使用递归检查数组是否为回文的程序