uva401(字符串)

题目:

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
B N Z5
C OO11
D P 2S
E3Q 3E
F R 4 
G S25Z
HHTT6 
IIUU7 
JLVV88
K WW9 
LJXX  

题意:回文(abccba)与镜像(3MIME)

思路:写两个函数分别判断回文与镜像,在main里调用。

#include<stdio.h>
#include<string.h>

bool f1(char a[]) {

	const int length=strlen(a);
	int l=length/2;
	char b[50]="\0";
	char c[50]="\0";
	if(length%2==0){
		for(int i=0;i<l;i++) {
			b[i]=a[l-i-1];
		}
		
		for(int j=0;j<l;j++) {
			c[j]=a[j+l];
		}
	}		
	else{
		for(int i=0;i<l;i++) {
			b[i]=a[l-i-1];
		}
		
		for(int j=0;j<l;j++) {
			c[j]=a[j+l+1];
		}
	}
	if(strcmp(b,c)==0)
		return true;
	else
		return false;
	
}
bool f2(char a[])
{

	int length=strlen(a);
	int l=length/2;
	for(int z=0;z<l+1;z++) {
		if(a[z]=='B'||a[z]=='N'||a[z]=='C'||a[z]=='D'||a[z]=='P'||a[z]=='Q'||a[z]=='F'||a[z]=='R'||a[z]=='4'||a[z]=='G'||a[z]=='6'||a[z]=='7'||a[z]=='K'||a[z]=='9')
			return false;
	}
	if(strlen(a)%2!=0) {
		if(a[l]=='A'||a[l]=='Y'||a[l]=='M'||a[l]=='O'||a[l]=='0'||a[l]=='1'||a[l]=='H'||a[l]=='T'||a[l]=='I'||a[l]=='U'||a[l]=='V'||a[l]=='8'||a[l]=='W'||a[l]=='X')
		{
			for(int i=0;i<l;i++) {
				if(a[i]=='A')
					if(a[length-i-1]!='A')
						return false;
				if(a[i]=='M')
					if(a[length-i-1]!='M')
						return false;
				if(a[i]=='Y')
					if(a[length-i-1]!='Y')
						return false;
				if(a[i]=='Z')
					if(a[length-i-1]!='5')
						return false;
				if(a[i]=='0')
					if(a[length-i-1]!='0')
						return false;	
				if(a[i]=='O')
					if(a[length-i-1]!='O')
						return false;
				if(a[i]=='1')
					if(a[length-i-1]!='1')
						return false;
				if(a[i]=='2')
					if(a[length-i-1]!='S')
						return false;
				if(a[i]=='S')
					if(a[length-i-1]!='2')
						return false;
				if(a[i]=='5')
					if(a[length-i-1]!='Z')
						return false;
				if(a[i]=='E')
					if(a[length-i-1]!='3')
						return false;
				if(a[i]=='3')
					if(a[length-i-1]!='E')
						return false;
				if(a[i]=='H')
					if(a[length-i-1]!='H')
						return false;
				if(a[i]=='T')
					if(a[length-i-1]!='T')
						return false;
				if(a[i]=='I')
					if(a[length-i-1]!='I')
						return false;
				if(a[i]=='U')
					if(a[length-i-1]!='U')
						return false;
				if(a[i]=='J')
					if(a[length-i-1]!='L')
						return false;
				if(a[i]=='L')
					if(a[length-i-1]!='J')
						return false;
				if(a[i]=='V')
					if(a[length-i-1]!='V')
						return false;
				if(a[i]=='8')
					if(a[length-i-1]!='8')
						return false;
				if(a[i]=='W')
					if(a[length-i-1]!='W')
						return false;
				if(a[i]=='X')
					if(a[length-i-1]!='X')
						return false;
			}
		}
		else
			return false;
	}
		else {
			for(int i=0;i<l;i++) {
				if(a[i]=='A')
					if(a[length-i-1]!='A')
						return false;
				if(a[i]=='M')
					if(a[length-i-1]!='M')
						return false;
				if(a[i]=='Y')
					if(a[length-i-1]!='Y')
						return false;
				if(a[i]=='Z')
					if(a[length-i-1]!='5')
						return false;
				if(a[i]=='0')
					if(a[length-i-1]!='0')
						return false;	
				if(a[i]=='O')
					if(a[length-i-1]!='O')
						return false;
				if(a[i]=='1')
					if(a[length-i-1]!='1')
						return false;
				if(a[i]=='2')
					if(a[length-i-1]!='S')
						return false;
				if(a[i]=='S')
					if(a[length-i-1]!='2')
						return false;
				if(a[i]=='5')
					if(a[length-i-1]!='Z')
						return false;
				if(a[i]=='E')
					if(a[length-i-1]!='3')
						return false;
				if(a[i]=='3')
					if(a[length-i-1]!='E')
						return false;
				if(a[i]=='H')
					if(a[length-i-1]!='H')
						return false;
				if(a[i]=='T')
					if(a[length-i-1]!='T')
						return false;
				if(a[i]=='I')
					if(a[length-i-1]!='I')
						return false;
				if(a[i]=='U')
					if(a[length-i-1]!='U')
						return false;
				if(a[i]=='J')
					if(a[length-i-1]!='L')
						return false;
				if(a[i]=='L')
					if(a[length-i-1]!='J')
						return false;
				if(a[i]=='V')
					if(a[length-i-1]!='V')
						return false;
				if(a[i]=='8')
					if(a[length-i-1]!='8')
						return false;
				if(a[i]=='W')
					if(a[length-i-1]!='W')
						return false;
				if(a[i]=='X')
					if(a[length-i-1]!='X')
						return false;
			}
		}
		return true;
	

}
int main() {
	char a[100];
	while(scanf("%s",a)!=EOF)
	{
		if(f1(a))
			if(f2(a))
				printf("%s -- is a mirrored palindrome.\n\n",a);
			else
				printf("%s -- is a regular palindrome.\n\n",a);
			else
				if(f2(a))
					printf("%s -- is a mirrored string.\n\n",a);
				else
					printf("%s -- is not a palindrome.\n\n",a);
	}
				return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值