算法竞赛入门经典第二版 回文词P49
#include<bits/stdc++.h> using namespace std; char rev[]="A 3 HIL JM O 2TUVWXY51SE Z 8 "; char *msg[]={"is not a palindrome","is a regular palindrome","is a mirrored string","is a mirrored palindrome"} ; char aa(char a){ if(isalpha(a)) return rev[a-'A'];//判断字符ch是否为英文字母,若为英文字母,返回非0(小写字母为2,大写字母为1)。若不是字母,返回0。 else return rev[a-'0'+25]; } int main(){ char s[1000]; while(scanf("%s",s)==1){ int p=1,m=1; int len=strlen(s); for(int i=0;i<(len+1)/2;i++){ if(s[i]!=s[len-1-i]) p=0;//回文串判断 if(aa(s[i])!=s[len-1-i]) m=0;//镜像串判断 镜像之后,是否和右边的一样 } printf("%s--%s.\n",s,msg[m*2+p]); } return 0; }
如果ch是大写字母,则ch-‘A’就是它在字母表中的序号,(A的序号为0,B为1)
如果ch为数字,那么ch-‘0’就是这个数值本身,' 5 ' - ' 0 ' =5;