c++ - 如何检查回文列表

标签 c++

我想知道如何编写一个函数来确定从波纹管代码中读取的列表是否为回文?例如,如果说数字列表是 123456,程序将打印出“这个列表不是回文”,如果列表是 12321,那么它会打印出“这个列表是回文”。

如有任何帮助,我们将不胜感激。

void popArray(int array1[]) {
    ifstream infile("TEST1.TXT");
    if (!infile) {
        cout << "Can't open file: " << endl;
        exit(EXIT_FAILURE);
    }
    for (int i = 0; i < 10; i++) {
        infile >> array1[i];
        cout << setw(2) << array1[i];
    }
}
void reverseList(int array1[]) {
    for (int x = 9; x > -1; x--) {
        cout << setw(2) << array1[x];
    }
}

最佳答案

我建议使用索引 i 并让它从 0size/2。然后在循环内比较 list[i] == list[(size - 1) - i ].

关于c++ - 如何检查回文列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16077160/

相关文章:

c++ - 将 C++ 中的列表对象移动到另一个对象

c++ - c.vim c/c++ 插件把函数的返回值放在函数上面,可以吗?

c++ - C++中函数重载的混淆

c++ - Qtimer isActive() 返回 false 但它正在毁掉

c++ - 无法将参数 2 从 'std::string *' 转换为 'std::string'

c++ - 如何使用 xlib 观看任何窗口移动?

c++ - OpenCL 程序构建错误 : source file is not valid UTF-8

C++ ncurses 编码 (UTF-8) 问题

c++ - malloc.c :2372: sysmalloc: Assertion

c++ - std::move() 的错误用法?