UVA401 第一个编程语言博客纪念一下
2015-07-20 20:11
302 查看
题目:
Description
A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right
as when the string is read from right to left.
A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is
a mirrored string because "A" and "I"are their own reverses, and "3" and "E" are each others' reverses.
A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string"ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the
original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.
Of course,"A","T", "O",
and "Y" are all their own reverses.
A list of all valid characters and their reverses is as follows.
Note that O (zero) and 0 (the letter) are considered the same character and therefore ONLY the letter "0" is a valid character.
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.
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
Sample Output
Hint
use the C++'s class of string will be convenient, but not a must
思路:
就是判断回文和镜像,对于镜像的判断,我是先过滤一遍有没有不符合镜像的字符,然后再判断符不符合镜像的要求。
Description
A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right
as when the string is read from right to left.
A mirrored string is a string for which when each of the elements of the string is changed to its reverse (if it has a reverse) and the string is read backwards the result is the same as the original string. For example, the string "3AIAE" is
a mirrored string because "A" and "I"are their own reverses, and "3" and "E" are each others' reverses.
A mirrored palindrome is a string that meets the criteria of a regular palindrome and the criteria of a mirrored string. The string"ATOYOTA" is a mirrored palindrome because if the string is read backwards, the string is the same as the
original and because if each of the characters is replaced by its reverse and the result is read backwards, the result is the same as the original string.
Of course,"A","T", "O",
and "Y" are all their own reverses.
A list of all valid characters and their reverses is as follows.
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 |
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 |
In addition, after each output line, you must print an empty line.
Sample Input
NOTAPALINDROME ISAPALINILAPASI 2A3MEAS ATOYOTA
Sample Output
NOTAPALINDROME -- is not a palindrome. ISAPALINILAPASI -- is a regular palindrome. 2A3MEAS -- is a mirrored string. ATOYOTA -- is a mirrored palindrome.
Hint
use the C++'s class of string will be convenient, but not a must
思路:
就是判断回文和镜像,对于镜像的判断,我是先过滤一遍有没有不符合镜像的字符,然后再判断符不符合镜像的要求。
<span style="font-family:FangSong_GB2312;font-size:18px;">#include<stdio.h> #include<string.h> //判断是否为回文 char str[1000]; int ispalindrome() { int i; int j; int z=strlen(str); for(i=0,j=z-1;i<j;i++,j--) { if(str[i]!=str[j]) { return 0; } } return 1; } int ismirrored() { int i; int z=strlen(str); int j=z/2; for(i=0;i<z;i++) { if(str[i]=='B'||str[i]=='C'||str[i]=='D'||str[i]=='F'||str[i]=='G'||str[i]=='K'||str[i]=='N'||str[i]=='P'||str[i]=='Q'||str[i]=='R'||str[i]=='4'||str[i]=='5'||str[i]=='6'||str[i]=='7'||str[i]=='9') return 0; } for (i = 0; i < j; i++) { if(str[i]=='A') if(str[z-1-i]!='A') return 0; if(str[i]=='E') if(str[z-1-i]!='3') return 0; if(str[i]=='H') if(str[z-1-i]!='H') return 0; if(str[i]=='I') if(str[z-1-i]!='I') return 0; if(str[i]=='J') if(str[z-1-i]!='L') return 0; if(str[i]=='L') if(str[z-1-i]!='J') return 0; if(str[i]=='M') if(str[z-1-i]!='M') return 0; if(str[i]=='O') if(str[z-1-i]!='O') return 0; if(str[i]=='S') if(str[z-1-i]!='2') return 0; if(str[i]=='T') if(str[z-1-i]!='T') return 0; if(str[i]=='U') if(str[z-1-i]!='U') return 0; if(str[i]=='V') if(str[z-1-i]!='V') return 0; if(str[i]=='W') if(str[z-1-i]!='W') return 0; if(str[i]=='X') if(str[z-1-i]!='X') return 0; if(str[i]=='Y') if(str[z-1-i]!='Y') return 0; if(str[i]=='Z') if(str[z-1-i]!='5') return 0; if(str[i]=='1') if(str[z-1-i]!='1') return 0; if(str[i]=='2') if(str[z-1-i]!='S') return 0; if(str[i]=='3') if(str[z-1-i]!='E') return 0; if(str[i]=='5') if(str[z-1-i]!='Z') return 0; if(str[i]=='8') if(str[z-1-i]!='8') return 0; if(str[i]=='0') if(str[z-1-i]!='0') return 0; } return 1; } int main() { int a; int b; while(scanf("%s",str)!=EOF) { a=ispalindrome(); b=ismirrored(); if(a==0&&b==0) { printf("%s -- is not a palindrome.",str); } if(a==1&&b==0) { printf("%s -- is a regular palindrome.",str); } if(a==0&&b==1) { printf("%s -- is a mirrored string.",str); } if(a==1&&b==1) { printf("%s -- is a mirrored palindrome.",str); } printf("\n\n"); } return 0; }</span>
相关文章推荐
- 12天学好C语言——记录我的C语言学习之路(Day 5)
- 利用基于@AspectJ的AOP实现权限控制
- 12天学好C语言——记录我的C语言学习之路(Day 5)
- java之设计模式---单例模式
- 【西祠日志】【04】还不错,今天asp基本功能完成了(2015.07.20周一)(第二周)
- [LeetCode][Java] Symmetric Tree
- day05--Java面向对象复习--Exception异常
- Qt的一些基础知识
- 【C语言学习】《C Primer Plus》第11章 字符串和字符串函数
- 7月20 学习记录(C语言)
- JAVA学习第五天
- JAVA中的使用Filter过滤器设置字符集
- C语言09 -- 高级指针
- [转]Python机器学习工具箱
- C语言08 -- 指针
- this指针的用法
- 好文-为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?
- C语言07 -- 结构体多文件管理
- C语言07 -- 结构体
- 【C++】运算符重载