本文共 3569 字,大约阅读时间需要 11 分钟。
Input
Input consists of strings (one per line) each of which will consist of one to twenty valid characters. There will be no invalid characters in any of the strings. Your program should read to the end of file.Output
For each input string, you should print the string starting in column 1 immediately followed by exactly one of the following strings.STRING CRITERIA
" – is not a palindrome." if the string is not a palindrome and is not a mirrored string " – is a regular palindrome." if the string is a palindrome and is not a mirrored string " – is a mirrored string." if the string is not a palindrome and is a mirrored string " – is a mirrored palindrome." if the string is a palindrome and is a mirrored string Note that the output line is to include the -'s and spacing exactly as shown in the table above and demonstrated in the Sample Output below. In addition, after each output line, you must print an empty line.Sample Input
NOTAPALINDROME ISAPALINILAPASI 2A3MEAS ATOYOTASample Output
NOTAPALINDROME – is not a palindrome.ISAPALINILAPASI – is a regular palindrome.
2A3MEAS – is a mirrored string.
ATOYOTA – is a mirrored palindrome.
思路:
用m,n来标记输入语句是否是回文和是否是镜像字符串。 假设输入语句是镜像回文,即:m=1,n=1; 通过遍历判断第i个字符和第x-i-1个字符是否相同, 如果出现不同,令m=0;标记为不是回文。 如果出现第i个字符的镜像不等于第x-i-1个字符,则令n=0;标记为不是镜像字符串。 最后只需判断m,n的情况,依次输出对应语句。#include#include #include using namespace std;const char arr[] = "A 3 HIL JM O 2TUVWXY51SE Z 8 ";int main(){ char str[21]; while (scanf("%s", str) == 1) { int x, m = 1, n = 1; x = strlen(str); for (int i = 0; i < x; i++) { if (str[i] != str[x - i - 1]) // 判断第i个字符是否和x-i-1位置的字符相同 { m = 0; } if (str[i] >= 'A'&&str[i] <= 'Z') { if (str[x - i - 1] != arr[str[i] - 'A']) // 判断第i个字符的镜像是否和x-i-1位置的字符相同 { n = 0; } } if (str[i] >= '0'&&str[i] <= '9') { if (str[x - i - 1] != arr[str[i] - '0' + 25]) // 判断第i个字符的镜像是否和x-i-1位置的字符相同 { n = 0; } } } cout << str; if (m == 0 && n == 0) cout << " -- is not a palindrome." << endl; if (m == 1 && n == 0) cout << " -- is a regular palindrome." << endl; if (m == 0 && n == 1) cout << " -- is a mirrored string." << endl; if (m == 1 && n == 1) cout << " -- is a mirrored palindrome." << endl; cout << endl; } return 0;}
转载地址:http://ogyof.baihongyu.com/