题意: 判断3种形式的回文 思路: 简单模拟(题意要清晰),判断是否反转回文函数要注意。 学习点: 字符对应,可以用两个数组。 源代码: //#define LOCAL #include <iostream> #include <string> #include <cstdlib> using namespace std; int isRegular(string s); int isMirrored(string s); int len; //定义两个string,原字符和反转字符一一对应。 string ori = "AEHIJLMOSTUVWXYZ12358"; string res = "A3HILJMO2TUVWXY51SEZ8"; int main() { string nStr; int nFlag1, nFlag2; #ifdef LOCAL freopen("1590.in", "r", stdin); #endif while (cin >> nStr) { len = nStr.size(); nFlag1 = isRegular(nStr); nFlag2 = isMirrored(nStr); cout << nStr; if (nFlag1 + nFlag2 == 2) cout << " -- is a mirrored palindrome./n"; else if (nFlag1) cout << " -- is a regular palindrome./n"; else if (nFlag2) cout << " -- is a mirrored string./n"; else cout << " -- is not a palindrome./n"; cout << endl; } return 0; } int isRegular(string s) { for (int i = 0; i <= len / 2; i ++) { if (s[i] == s[len - 1 - i]) continue; return 0; } return 1; } int isMirrored(string s) { int i, j; for (i = 0; i <= len / 2; i ++) { for (j = 0; j < 21; j ++) { if (s[i] == ori[j]) if (res[j] == s[len - 1 - i]) break; } if (j == 21) return 0; } return 1; }