[C++]迴文字符串判斷

Normal Version

#include <iostream>
#include <string>
using namespace std;
inline bool is_palindrome(const string str) {
    int length = str.length();
    for (int i = 0; i < length / 2; ++i)
        if (str[i] != str[length - i - 1])
            return false;
    return true;
};
int main(int argc, char *argv[]) {
    ios::sync_with_stdio(false);
    string str; cin >> str;
    if (is_palindrome(str)) cout << "YES";
    else cout << "NO"; cout.put('\n');
    return 0;
};

Simple Version

#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    ios::sync_with_stdio(false);
    string str1, str2; 
    cin >> str1; str2 = str1;
    reverse(str1.begin(), str1.end());
    if (str1 == str2) cout << "YES";
    else cout << "NO"; cout.put('\n');
    return 0;
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章