hdoj 1318 Palindromes(模拟)

思路:按照题目描述,进行三种情况的模拟,先对输入的字符串进行第三种情况的判断,如果不是第三种,再分别判断后两者,如果都不是,再输出no;(注意的是字符串在函数中的处理情况,不注意会导致函数的实参变化,从而导致错误);


代码如下:

#include <iostream>  
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
using namespace std;
int pal(char *a)
{
	int i;
	int len;
	int x;
	char b[30];
	len = strlen(a);
	x = 0;
	for (i = len - 1; i >= 0; i--)
	{
		b[x++] = a[i];
	}
	b[x] = '\0';
	if (!strcmp(a, b))
	{
		return 1;
	}
	else
		return 0;
}
int mir(char *b)
{
	int i;
	int len;
	int flag;
	char a[30];
	char temp[30];
	int x;
	len = strlen(b);
	flag = 1;
	x = 0;
	strcpy(a, b);
	for (i = 0; i < len; i++)
	{
		if (a[i] == 'A')
			a[i] = 'A';
		else if (a[i] == 'E')
			a[i] = '3';
		else if (a[i] == 'H')
			a[i] = 'H';
		else if (a[i] == 'I')
			a[i] = 'I';
		else if (a[i] == 'J')
			a[i] = 'L';
		else if (a[i] == 'L')
			a[i] = 'J';
		else if (a[i] == 'M')
			a[i] = 'M';
		else if (a[i] == 'O')
			a[i] = 'O';
		else if (a[i] == 'S')
			a[i] = '2';
		else if (a[i] == 'T')
			a[i] = 'T';
		else if (a[i] == 'U')
			a[i] = 'U';
		else if (a[i] == 'V')
			a[i] = 'V';
		else if (a[i] == 'W')
			a[i] = 'W';
		else if (a[i] == 'X')
			a[i] = 'X';
		else if (a[i] == 'Y')
			a[i] = 'Y';
		else if (a[i] == 'Z')
			a[i] = '5';
		else if (a[i] == '1')
			a[i] = '1';
		else if (a[i] == '2')
			a[i] = 'S';
		else if (a[i] == '3')
			a[i] = 'E';
		else if (a[i] == '5')
			a[i] = 'Z';
		else if (a[i] == '8')
			a[i] = '8';
		else
		{
			flag = 0;
			break;
		}
	}
	for (i = len - 1; i >= 0; i--)
	{
		temp[x++] = a[i];
	}
	temp[x] = '\0';
	if (flag)
		if (!strcmp(temp, b))
			return 1;
		else
			return 0;
	else
		return 0;
}
int mp(char *a)
{
	if (pal(a) && mir(a))
		return 1;
	else
		return 0;

}
int main()
{
	char b[30];
	char a[30];
	char c[30];
	char d[30];
	char e[30];
	int t;
	t = 0;
	while (cin >> b)
	{
	
		strcpy(a, b);
		strcpy(c, b);
		strcpy(d, b);
		strcpy(e, b);
		if (mp(a))
			printf("%s -- is a mirrored palindrome.\n", a);
		else
		{
			
			if (pal(c))
			{
				printf("%s -- is a regular palindrome.\n", c);
			}
				
			else if (mir(d))
				printf("%s -- is a mirrored string.\n", d);
			else
				printf("%s -- is not a palindrome.\n", e);
		}
		printf("\n");
			
	}
	
	system("pause");
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值