回文词(UVa401)

  详细题目描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=342

输出表:

  

 

C++11代码如下:

 1 #include<iostream>
 2 #include<string.h>
 3 #include<ctype.h>
 4 using namespace std;
 5 const char* rev = "A   3  HIL JM O   2TUVWXY51SE Z  8 "; //指针名可直接当数组名使用,均指代地址
 6 const char* msg[] = { "not a palindrome","a regular palindrome",  //二维数组,msg前面要加*
 7                     "a mirrored string","a mirrored palindrome" };
 8 //四种组合:既不是回文也不是镜像、只是回文、只是镜像、既是回文又是镜像
 9 
10 char mirror(char ch) {  //求字母或数字的镜像
11     if (isalpha(ch)) return rev[ch - 'A'];
12     return rev[ch - '0' + 25];
13 }
14 
15 int main() {
16     char s[30];
17     while (cin >> s) {
18         int len = strlen(s);
19         int m = 1, p = 1;
20         for (int i = 0; i <= (len / 2); i++) {
21             if (s[i] != s[len - 1 - i]) p = 0;
22             if (mirror(s[i]) != s[len - 1 - i]) m = 0;
23         }
24         cout << s << " -- is " << msg[m * 2 + p] << '.' << endl << endl;
25     }
26     return 0;
27 }
posted on 2018-06-25 14:21  Pink.Pig  阅读(261)  评论(0编辑  收藏  举报