UVA - 401 - Palindromes

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值