想法:
處理鏡像的部份我是把它存成陣列,Mir[]="AAE3HHIIJLLJ....",比對兩個字元是否為鏡像,則看Mir[i]和Mir[i+1]是否符合,另外要注意本題每個輸出之間還要空一行。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
#include <cstring> | |
using namespace std; | |
char Mir[]="AAE3HHIIJLLJMMOOS2TTUUVVWWXXYYZ500112S3E5Z88"; | |
bool isMirrored (char a, char b){ | |
for (int i=0; Mir[i+1]; i++) | |
if (Mir[i]==a && Mir[i+1]==b) return 1; | |
return 0; | |
} | |
bool isPalindrome (char a,char b){ | |
return a == b; | |
} | |
int main() | |
{ | |
// freopen("input.txt","rt",stdin); | |
char c[100]; | |
while (gets(c)){ | |
bool palindrome = 1,mirrored = 1; | |
for (int i = 0, j = strlen(c)-1; i <= j; i++, j--){ | |
if (!isMirrored(c[i],c[j])) mirrored = 0; | |
if (!isPalindrome(c[i],c[j])) palindrome = 0; | |
} | |
if (palindrome && mirrored) printf("%s -- is a mirrored palindrome.\n\n",c); | |
else if (palindrome) printf("%s -- is a regular palindrome.\n\n",c); | |
else if (mirrored) printf("%s -- is a mirrored string.\n\n",c); | |
else printf("%s -- is not a palindrome.\n\n",c); | |
} | |
return 0; | |
} |
沒有留言:
張貼留言