UVa401 Palindromes

tech2022-08-11  127

UVa401 Palindromes

#include <cstdio> #include <cctype> #include <cstring> const char* reverse = "A 3 HIL JM O 2TUVWXY51SE Z 8 "; const char* msg[] = {"not a palindrome.", "a regular palindrome.", "a mirrored palindrome.", "a mirrored string."}; bool isMirrored(const char c1, const char c2) { if (isdigit(c1)) { //c1为数字 return reverse[c1-'0'+25] == c2; } else { //c1为字母 return reverse[c1-'A'] == c2; } } int type(const char* str) { bool isPal = true; bool isMir = true; int len = strlen(str); for (size_t i = 0; i <= len/2; i++) { if (str[i] != str[len-i-1]) isPal = false; if (!isMirrored(str[i], str[len-i-1])) isMir = false; if (!(isPal || isMir)) break; //既不是回文串也不是镜像串,结束判断。 } if (isPal + isMir == 1) { if (isPal) { // isPal==ture and isMir == false return 1; }else { // isPal==false and isMir == true return 3; } } else { return isPal + isMir; } } int main() { // freopen("./in.txt","r",stdin); // freopen("./out.txt","w",stdout); char str[25]; while ( scanf("%s", str) != EOF) { printf("%s -- is %s\n\n", str, msg[type(str)]); } return 0; }
最新回复(0)