您的位置:首页 > 编程语言

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.

CharacterReverseCharacterReverseCharacterReverse
AAMMYY
BNZ5
COO11
DP2S
E3Q3E
FR4
GS25Z
HHTT6
IIUU7
JLVV88
KWW9
LJXX
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.

STRINGCRITERIA
" -- 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
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: