【发布时间】:2015-03-29 12:57:27
【问题描述】:
我需要找到位于原始数组中间的子数组并检查它是否是回文。之后我需要打印数组的起始索引 -1 和结束索引。
我试着去做,但结果不是我所期望的。 你能指出我犯的任何错误吗?
#include <iostream>
using namespace std;
void print_sub_pals(int *nums,int length)
{
for (int i = 0; i < (length / 2); ++i)
{
for (int j = length -1 ; j < (length/2); j--)
{
int start = *(nums + i);
int end = *(nums + j);
if ((start) == (end))
{
cout << start - 1 << endl;
cout << end << endl;
}
else
{
cout << "-1" << endl;
}
}
}
}
int main()
{
int len = 7;
int arr[7] = { 1, 2, 3, 4, 3, 6, 7 };
print_sub_pals(arr, len);
}
【问题讨论】:
-
I tried to do it but the outcome is not what I expected。相反,您能明确说明您的问题吗? -
第二个for循环条件
j < (length/2);不对 -
抱歉没有把我的问题解释清楚。我试图找到子数组并检查它是否是回文,但输出没有打印任何内容。我想我的数组可能做错了。
-
当您使用调试器时,哪一行导致了问题?变量的值是多少?代码流程是否正确?
标签: c++ pointers palindrome