fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool palindrome(const int arr[], int n)
  5. {
  6. if (n < 1)
  7. return false;
  8.  
  9. int middle = n / 2; --n;
  10. for (int i = 0; i < middle; ++i) {
  11. if (arr[i] != arr[n-i]) {
  12. return false;
  13. }
  14. }
  15.  
  16. return true;
  17. }
  18.  
  19. template<size_t N>
  20. bool palindrome(const int (&arr)[N])
  21. {
  22. return palindrome(arr, N);
  23. }
  24.  
  25. int main()
  26. {
  27. int arr1[] = { 1, 2, 3, 2, 1 };
  28. cout << palindrome(arr1) << endl;
  29. // true
  30.  
  31. int arr2[] = { 1, 2, 3, 3, 2, 1 };
  32. cout << palindrome(arr2) << endl;
  33. // true
  34.  
  35. int arr3[] = { 1, 2, 3, 2, 1, 5, 6, 6, 5 };
  36. cout << palindrome(arr3) << endl;
  37. // false
  38.  
  39. int arr4[] = { 1, 2, 3, 2, 1, 5, 6, 7, 6, 5 };
  40. cout << palindrome(arr4) << endl;
  41. // false
  42.  
  43. int arr5[] = { 1, 2, 3, 3, 1, 5, 6, 6, 5 };
  44. cout << palindrome(arr5) << endl;
  45. // false
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0.01s 5312KB
stdin
Standard input is empty
stdout
1
1
0
0
0