歡迎加入我的 Discord 群組與我討論程式相關的問題!

Posted on 

 by 

 in 

ZeroJudge a022. 迴文

評分:1 分,滿分為 5。

題目連結

題意

給一個字串,問字串是不是迴文。迴文的定義為正向,反向讀到的字串均相同

解題方法

題目有說到迴文的定義是正著讀跟反著讀會是一樣的,那我們就用 c++ <algorithm> 裡面的 reverse 函數真的把字串轉過來再看跟原本一不一樣就可以了!

#include <bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    string s;
    cin >> s;
    string t = s;
    reverse(s.begin(), s.end());
    if(s == t) cout << "yes\n";
    else cout << "no\n";
}

圖片來源:https://favtutor.com/blogs/palindrome-pairs

發表迴響

Blog at WordPress.com.

%d 位部落客按了讚: