回文字镜像字 (Palindromes, UVa401)
2017-01-13 21:16
405 查看
代码如下,练手用。
#include <iostream> #include <stdio.h> #include <string.h> #include <ctype.h> using namespace std; //常量字符串进行替换和格式化输出消息非常的方便 const char* rev = "A 3 HIL JM O 2TUVWXY51SE Z 8 "; const char* msg[] = { "is not a palindrom.", "is a regular palindrom.", "is a mirrored string.", "is a mirrored palindrome." }; char mirr(char chr) { if (isalpha(chr)) { return rev[chr - 'A']; } else if (isdigit(chr) && chr != '0') { return rev[chr - '1' + 26]; } else { return chr; } } int main() { char s[30]; while (scanf("%s", s) == 1) //输入有空格,Tab分割的用scanf()方便 { int len = strlen(s); int ok_palindrom = 1, ok_mirrored = 1; for (int i = 0; i <= (len - 1) / 2; i++) { if (s[i] != s[len - 1 - i]) { ok_palindrom = 0; } if (s[i] != mirr(s[len - 1 - i])) { ok_mirrored = 0; } } printf("%s -- %s", s, msg[ok_mirrored * 2 + ok_palindrom]); // 也可以用此种简单方法格式化输出 // if (!ok_palindrom && !ok_mirrored) // printf("%s -- is not a palindrome.\n\n", s); // if (ok_palindrom && !ok_mirrored) // printf("%s -- is a regular palindrome.\n\n", s); // if (!ok_palindrom && ok_mirrored) // printf("%s -- is a mirrored string.\n\n", s); // if (ok_palindrom && ok_mirrored) // printf("%s -- is a mirrored palindrome.\n\n", s); } return 0; }
相关文章推荐
- UVa401 - Palindromes-字符串(镜像字符串)-难度2
- 《算法竞赛入门经典2ndEdition 》例题3-3 回文词(Palindromes, Uva401)
- UVa 401 Palindromes (回文串与镜像串的综合判断)
- HDU 1544 Palindromes(判断是否为回文序列和镜像序列)
- 镜像命令MI(MIRROR)操作时,如何使文字不翻转
- UVA401 - Palindromes
- 算法竞赛 例3-3回文词(Palindromes,UVa401)
- UVa401:Palindromes
- 回文词(Palindromes, UVa401)
- 判断一个字符串是否为回文串和镜像串(Palindromes)
- UVa 401 Palindromes(镜像回文字符串)
- HDOJ1318 Palindromes(镜像回文字符串)
- Palindromes - uva401
- UVa 401 Palindromes(镜像回文字符串)
- 回文串和镜像串,组合不同,下标为何*2 。 UVA 401 Palindromes 回文词
- UVA401 -Palindromes
- UVA 401 - Palindromes(判断回文串和镜像串)
- 回文词(Palindromes, UVa401)
- 联想Y470 非虚拟机安装苹果Mac OS X Mavericks 10.9.1教程详解(文字+图片),通俗易懂亲自动手——序列二之镜像写入,引导建立
- uva401 - Palindromes