您的位置:首页 > 其它

UVA - 401 - Palindromes

2014-04-12 13:56 363 查看
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem&problem=342

题意:

辨别输入的字符串是否对称或自身镜像。

解题:

本是水题,可给行输入搞晕了。检查了半天。

cin.getline WA,但gets就能过,但gets是系统不推荐使用的。

解题用gets,其实用cin.getline。

#include <iostream>
#include <cstring>
#include <stdio.h>

using namespace std;

const int MAXLENGTH = 20;

int main()
{
char LETTER_REVERSE[26] =
{
'A', ' ', ' ', ' ', '3', ' ', ' ',
'H', 'I', 'L', ' ', 'J', 'M', ' ',
'O', ' ', ' ', ' ', '2', 'T',
'U', 'V', 'W', 'X', 'Y', '5'
};

char NUMBER_REVERSE[10] =
{
' ', '1', 'S', 'E', ' ', 'Z', ' ', ' ', '8', ' '
};
// WA
//	char str[100];
//	while ( fgets(str, 100, stdin) != NULL )
// WA
// 	char str[100];
// 	while ( cin.getline(str, MAXLENGTH) )
// WA
// 	string str;
// 	while ( getline(cin, str) )
char str[100];
while ( gets(str) )
{
bool bMirror = true;
bool bPalindrome = true;
// 		for ( int i=0, j=str.length()-1; i<=j; i++, j-- )
for ( int i=0, j=strlen(str)-1; i<=j; i++, j-- )
{
if ( str[i] != str[j] )
{
bPalindrome = false;
} // end if
if ( isalpha(str[i]) )
{
if ( LETTER_REVERSE[str[i]-'A'] != str[j] )
{
bMirror = false;
} // end if
} // end if
else
{
if ( NUMBER_REVERSE[str[i]-'0'] != str[j] )
{
bMirror = false;
} // end if
} // end else
} // end for

cout <<str;
if ( bMirror && bPalindrome )
{
cout <<" -- is a mirrored palindrome." <<'\n';
} // end if
else
{
if ( bMirror && !bPalindrome )
{
cout <<" -- is a mirrored string." <<'\n';
} // end if
else
{
if ( !bMirror && bPalindrome )
{
cout <<" -- is a regular palindrome." <<'\n';
} // end if
else
{
cout <<" -- is not a palindrome." <<'\n';
} // end else
} // end else
} // end else

cout <<'\n';

} // end while

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: