题目:
A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.
A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is a mirrored string because "A" and "I"are their own reverses, and "3" and "E" are each others' reverses.
A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string"ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.
Of course,"A","T", "O", and "Y" are all their own reverses.
A list of all valid characters and their reverses is as follows.
Character | Reverse | Character | Reverse | Character | Reverse |
A | A | M | M | Y | Y |
B | N | Z | 5 | ||
C | O | O | 1 | 1 | |
D | P | 2 | S | ||
E | 3 | Q | 3 | E | |
F | R | 4 | |||
G | S | 2 | 5 | Z | |
H | H | T | T | 6 | |
I | I | U | U | 7 | |
J | L | V | V | 8 | 8 |
K | W | W | 9 | ||
L | J | X | X |
题意:回文(abccba)与镜像(3MIME)
思路:写两个函数分别判断回文与镜像,在main里调用。
#include<stdio.h>
#include<string.h>
bool f1(char a[]) {
const int length=strlen(a);
int l=length/2;
char b[50]="\0";
char c[50]="\0";
if(length%2==0){
for(int i=0;i<l;i++) {
b[i]=a[l-i-1];
}
for(int j=0;j<l;j++) {
c[j]=a[j+l];
}
}
else{
for(int i=0;i<l;i++) {
b[i]=a[l-i-1];
}
for(int j=0;j<l;j++) {
c[j]=a[j+l+1];
}
}
if(strcmp(b,c)==0)
return true;
else
return false;
}
bool f2(char a[])
{
int length=strlen(a);
int l=length/2;
for(int z=0;z<l+1;z++) {
if(a[z]=='B'||a[z]=='N'||a[z]=='C'||a[z]=='D'||a[z]=='P'||a[z]=='Q'||a[z]=='F'||a[z]=='R'||a[z]=='4'||a[z]=='G'||a[z]=='6'||a[z]=='7'||a[z]=='K'||a[z]=='9')
return false;
}
if(strlen(a)%2!=0) {
if(a[l]=='A'||a[l]=='Y'||a[l]=='M'||a[l]=='O'||a[l]=='0'||a[l]=='1'||a[l]=='H'||a[l]=='T'||a[l]=='I'||a[l]=='U'||a[l]=='V'||a[l]=='8'||a[l]=='W'||a[l]=='X')
{
for(int i=0;i<l;i++) {
if(a[i]=='A')
if(a[length-i-1]!='A')
return false;
if(a[i]=='M')
if(a[length-i-1]!='M')
return false;
if(a[i]=='Y')
if(a[length-i-1]!='Y')
return false;
if(a[i]=='Z')
if(a[length-i-1]!='5')
return false;
if(a[i]=='0')
if(a[length-i-1]!='0')
return false;
if(a[i]=='O')
if(a[length-i-1]!='O')
return false;
if(a[i]=='1')
if(a[length-i-1]!='1')
return false;
if(a[i]=='2')
if(a[length-i-1]!='S')
return false;
if(a[i]=='S')
if(a[length-i-1]!='2')
return false;
if(a[i]=='5')
if(a[length-i-1]!='Z')
return false;
if(a[i]=='E')
if(a[length-i-1]!='3')
return false;
if(a[i]=='3')
if(a[length-i-1]!='E')
return false;
if(a[i]=='H')
if(a[length-i-1]!='H')
return false;
if(a[i]=='T')
if(a[length-i-1]!='T')
return false;
if(a[i]=='I')
if(a[length-i-1]!='I')
return false;
if(a[i]=='U')
if(a[length-i-1]!='U')
return false;
if(a[i]=='J')
if(a[length-i-1]!='L')
return false;
if(a[i]=='L')
if(a[length-i-1]!='J')
return false;
if(a[i]=='V')
if(a[length-i-1]!='V')
return false;
if(a[i]=='8')
if(a[length-i-1]!='8')
return false;
if(a[i]=='W')
if(a[length-i-1]!='W')
return false;
if(a[i]=='X')
if(a[length-i-1]!='X')
return false;
}
}
else
return false;
}
else {
for(int i=0;i<l;i++) {
if(a[i]=='A')
if(a[length-i-1]!='A')
return false;
if(a[i]=='M')
if(a[length-i-1]!='M')
return false;
if(a[i]=='Y')
if(a[length-i-1]!='Y')
return false;
if(a[i]=='Z')
if(a[length-i-1]!='5')
return false;
if(a[i]=='0')
if(a[length-i-1]!='0')
return false;
if(a[i]=='O')
if(a[length-i-1]!='O')
return false;
if(a[i]=='1')
if(a[length-i-1]!='1')
return false;
if(a[i]=='2')
if(a[length-i-1]!='S')
return false;
if(a[i]=='S')
if(a[length-i-1]!='2')
return false;
if(a[i]=='5')
if(a[length-i-1]!='Z')
return false;
if(a[i]=='E')
if(a[length-i-1]!='3')
return false;
if(a[i]=='3')
if(a[length-i-1]!='E')
return false;
if(a[i]=='H')
if(a[length-i-1]!='H')
return false;
if(a[i]=='T')
if(a[length-i-1]!='T')
return false;
if(a[i]=='I')
if(a[length-i-1]!='I')
return false;
if(a[i]=='U')
if(a[length-i-1]!='U')
return false;
if(a[i]=='J')
if(a[length-i-1]!='L')
return false;
if(a[i]=='L')
if(a[length-i-1]!='J')
return false;
if(a[i]=='V')
if(a[length-i-1]!='V')
return false;
if(a[i]=='8')
if(a[length-i-1]!='8')
return false;
if(a[i]=='W')
if(a[length-i-1]!='W')
return false;
if(a[i]=='X')
if(a[length-i-1]!='X')
return false;
}
}
return true;
}
int main() {
char a[100];
while(scanf("%s",a)!=EOF)
{
if(f1(a))
if(f2(a))
printf("%s -- is a mirrored palindrome.\n\n",a);
else
printf("%s -- is a regular palindrome.\n\n",a);
else
if(f2(a))
printf("%s -- is a mirrored string.\n\n",a);
else
printf("%s -- is not a palindrome.\n\n",a);
}
return 0;
}