好久没有刷题了 感觉题目比较简单 锻炼思维的严谨性 看来刷题还是很有必要的
贴上源码
#include<iostream> #include<fstream> #include<cstring> #include<string> using namespace std; //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 //判0与o的时候要注意哦; char one[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789"; char two[] = "A 3 HIL JM O 2TUVWXY51SE Z 8 "; char char_mirror[26]= { 'A','*','*','*','3','*','*','H','I','L','*','J','M', '*','O','*','*','R','2','T','U','V','W','X','Y','5' }; char num_mirror[10]={'0','1','S','E','*','Z','*','*','8','*'}; int check_huiwen(char * str) { int len = strlen(str); for (int i = 0; i < len/2; i++) { if (str[i] == '0' && str[len-i-1] == 'O' || str[i] == 'O' && str[len-i-1] == '0') { continue; } if (str[len-i-1] != str[i]) return -1; } return 0; } int check_mirror(char * str) { int len = strlen(str); for (int i = 0; i < len/2+1; i++) { if (isalpha(str[i]) && str[len-i-1] == char_mirror[str[i]-'A'] || isdigit(str[i]) && num_mirror[str[i]-'0'] == str[len-i-1]) { continue; } else { if (str[i] == '0' && str[len - 1 - i] == 'O' || str[i] == 'O' && str[len - 1 - i] == '0') continue; return -1; } } return 0; } int main() { #ifndef ONLINE_JUDGE fstream cin("D://code//acm//txt//4.txt"); #endif char str[25]; while (cin>>str) { int is_huiwen = 0; int is_mirror = 0; if ( check_huiwen(str) == 0 ) { is_huiwen = 1; } if ( check_mirror(str) == 0 ) { is_mirror = 1; } if (is_huiwen && is_mirror ) { cout<<str<<" -- is a mirrored palindrome."<<endl<<endl; } else if (is_huiwen && ! is_mirror) { cout<<str<<" -- is a regular palindrome."<<endl<<endl; } else if (is_mirror && ! is_huiwen) { cout<<str<<" -- is a mirrored string."<<endl<<endl; } else if (!is_huiwen && ! is_mirror) { cout<<str<<" -- is not a palindrome."<<endl<<endl; } } return 0; }这道题目不难却刷了很久
原因:
1.样例超过去之后 每一行都多复制了一些空格,以后注意
2.check_mirror函数for循环的终止条件i<len/2+1,中间点容易被忽视,这个也可以看作边界条件,以后注意
3.为什么会出现第二个问题呢 因为在写check_huiwen时 就写的 i<len/2 就顺手抄了过来 不多思考 两个问题还是不一样的 细节没把握好,以后参考以前的代码时一定注意边界条件
4.isdigit函数没有想到;
5.#ifndef ONLINE_JUDGE #endif
6.