演算法:
- 頭尾的兩個字元進行倆倆判斷
- 若有不同時,則不是回文。
- 若相同則往字串中間移動繼續倆倆判斷。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void msg(bool b); | |
bool isPalindrome( const string &s ); | |
int main() | |
{ | |
string str; | |
while(cin >> str) | |
{ | |
msg(isPalindrome(str)); | |
} | |
return 0; | |
} | |
void msg(bool b) | |
{ | |
cout << (b?"yes":"no") << endl; | |
} | |
bool isPalindrome( const string &s ) | |
{ | |
short len = s.length(); | |
for(short i = 0; i < len / 2; i++) | |
{ | |
if( s[i] != s[len - 1 - i] ) | |
return false; | |
} | |
return true; | |
} |
沒有留言:
張貼留言