题目大意:给出一个字符串,判断他是否镜像和回文
解题思路:设置数组判断是否镜像,根据相等判断是否相等
#include<cstdio>
#include<cstring>
char m1[36]="ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char m2[36]="A 3 HIL JM O 2TUVWXY51SE Z 8 ";
int judge_p(char *str, int len) {
if( len == 1)//如果长度为1,表示回文
return 1;
for( int i = 0; i < len / 2; i++)//按照对称进行比较,相等继续,不等返回
if(str[i] == str[len - 1 - i])
continue;
else
return 0;
return 1;//如果全部符合则返回1
}
int judge_m(char *str, int len) {
int j;
int mark = 0;
if(len == 1) {
for(int i = 0; i < 36; i++)
if(str[0] == m2[i] )
return 1;
return 0;
}
if(len % 2 == 0)
mark = 1;
else
mark = 0;
char t = str[len / 2];
if(mark == 0){
if(t == 'A' || t == 'H' || t == 'I' || t == 'M' || t == 'O' || t == 'T' || t == 'U' || t == 'V' || t == 'W'|| t == 'X' || t == 'Y' || t == '1' || t == '8') ;
else
return 0;
}
for(int i = 0; i < len / 2; i++) {
for( j = 0; j < 36; j++)
if(str[i] == m1[j] && str[len - i - 1] == m2[j])
break;
if(j == 36)
return 0;
}
return 1;
}
/*
int judge_m(char *str, int len) {
int mark ;
for(int i = 0; i < len / 2; i++) {
switch(str[i]) {
case 'A': if(str[len - 1 - i] == 'A')
mark = 0;
else
mark = 1;
break;
case 'E': if(str[len - 1 - i] == '3')
mark = 0;
else
mark = 1;
break;
case 'H': if(str[len - 1 - i] == 'H')
mark = 0;
else
mark = 1;
break;
case 'I': if(str[len - 1 - i] == 'I')
mark = 0;
else
mark = 1;
break;
case 'J': if(str[len - 1 - i] == 'L')
mark = 0;
else
mark = 1;
break;
case 'L': if(str[len - 1 - i] == 'J')
mark = 0;
else
mark = 1;
break;
case 'M': if(str[len - 1 - i] == 'M')
mark = 0;
else
mark = 1;
break;
case 'O': if(str[len - 1 - i] == 'O')
mark = 0;
else
mark = 1;
break;
case 'S': if(str[len - 1 - i] == '2')
mark = 0;
else
mark = 1;
break;
case 'T': if(str[len - 1 - i] == 'T')
mark = 0;
else
mark = 1;
break;
case 'U': if(str[len - 1 - i] == 'U')
mark = 0;
else
mark = 1;
break;
case 'V': if(str[len - 1 - i] == 'V')
mark = 0;
else
mark = 1;
break;
case 'W': if(str[len - 1 - i] == 'W')
mark = 0;
else
mark = 1;
break;
case 'X': if(str[len - 1 - i] == 'X')
mark = 0;
else
mark = 1;
break;
case 'Y': if(str[len - 1 - i] == 'Y')
mark = 0;
else
mark = 1;
break;
case 'Z': if(str[len - 1 - i] == '5')
mark = 0;
else
mark = 1;
break;
case '1': if(str[len - 1 - i] == '1')
mark = 0;
else
mark = 1;
break;
case '2': if(str[len - 1 - i] == 'S')
mark = 0;
else
mark = 1;
break;
case '3': if(str[len - 1 - i] == 'E')
mark = 0;
else
mark = 1;
break;
case '5': if(str[len - 1 - i] == 'Z')
mark = 0;
else
mark = 1;
break;
case '8': if(str[len - 1 - i] == '8')
mark = 0;
else
mark = 1;
break;
deafult: mark = 1;
}
if(mark == 1) //如果发现一个不回文,则返回领
return 0;
}
return 1;//如果全部检测完了表示过了.返回1
}
*/
int main() {
char str[50];
int m;
int p;
int len;
char str2[50];
while(gets(str)) {
len = strlen(str);
strcpy(str2,str);
for(int i = 0; i < len; i++)
if(str[i] == '0')
str[i] = 'O';
m = judge_m(str,len);
p = judge_p(str,len);
if(m == 0 && p == 0)
printf("%s -- is not a palindrome.\n\n", str2) ;
if(m == 0 && p == 1)
printf("%s -- is a regular palindrome.\n\n", str2);
if(m == 1 && p == 0)
printf("%s -- is a mirrored string.\n\n", str2);
if(m == 1 && p == 1)
printf("%s -- is a mirrored palindrome.\n\n",str2);
memset(str,'\0',sizeof(str));
}
return 0;
}