zoukankan      html  css  js  c++  java
  • 一天一算法:回文判断

    问题描述:

    什么是回文?如,aha, adda,单ahah就不是回文,等等

    如何判断一串字符串是回文呢?

    这里的想法是:我们利用队列的方式,找到字符的中间的位置,将中间字符之前的全部入栈,然后全部出栈,与中间字符之后的字符进行比较,如果全部一样,那么就是回文。

    代码:

    #include<iostream>
    #include <queue>
    #include <string.h>
    using namespace std;
    
    int main()
    {
        char str[] = "ahaha";
        char tmp[10] = {0};
        int middle = sizeof(str)/sizeof(char) / 2 -1 ;
    
        queue<char> str_queue;
        for (int i = middle - 1; i >= 0; i--) {
            str_queue.push(str[i]);
        }
        
        int i = 0;
        while (!str_queue.empty()) {
            tmp[i] = str_queue.front();
            str_queue.pop();
            i++;
        }
        tmp[i] = '';
        
        if (memcmp(str+3, tmp, 2) == 0 ) {
            cout << "Yes" << endl;
        } else {
            cout  << "No " << endl;
        }
    
    }
  • 相关阅读:
    Android Studio连接真机
    day 4 __all__ 包 __init__.py
    day1 创建X00001文件1K
    day 3 模块
    day 2 异常传递 ,抛出
    day 1 异常基本功能
    day 7 __new___
    day 6 汽车4S店铺
    day 5 多态 类 静态
    day 4 继承
  • 原文地址:https://www.cnblogs.com/457220157-FTD/p/4063549.html
Copyright © 2011-2022 走看看