我使用以下代码进行整数数组的回文检查,并使用变量'declare'的值作为回文检查。我使用的技术是,如果声明在结尾是1,则数组是回文,否则不是。但它不起作用。在代码的最后,它始终保持declare的值,该值是初始化的,与代码的其余部分无关。请调试。
#include <iostream>
using namespace std;
void main()
{
int array1[3] = {0,0,1};
int j = 2;
cout << "Given Array is:\n";
for (int i = 0; i < 3; i++)
cout << array1[i];
cout << endl;
int determiner[3];
for (int i = 0; i <3; i++){
determiner[j] = array1[i];
j -= 1;
}
cout << "Reversed Array is:\n";
for (int i = 0; i < 3; i++)
cout << determiner[i];
cout << endl;
int declare;
for (int u = 0; u < 3; u++)
{
if (array1[u] = determiner[u])
{
declare = 1;
}
if (array1[u] != determiner[u])
{
declare = 0;
break;
}
}
cout << endl;
cout << declare<< endl;
if (declare==1)
cout << "Given Array is Palindrome. Cheers!!!\n";
if (declare==0)
cout << "Emhmm! This aint Palindrome.\n";
system("pause");
}
答案 0 :(得分:1)
if(array1 [u] = determininer [u])
应该是
if(array1 [u] == determininer [u])