uva--401 Palindromes

题目:Palindromes

解题思路:判断是否为回文:判断字符串前n/2的第i个字符与第n-1-i个字符是否相同,都相同就是回文;判断是否为mirrored,判断字符串前n/2字符的第n-1-i个字符是否与第i个的镜像字符相同,都相同则是mirrored;要注意每个输出后面都要再打印一行空白行;

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

const int N = 50;
char *s1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
char *s2 = "A000300HIL0JM0O0002TUVWXY51SE0Z0080";

bool ispalindrome(char * s) {

	int i, n = strlen(s);
	for (i = 0;i <= n/2; i++)
		if (s[i] != s[n-1-i])
			return false;
		return true;
}
bool ismirrored (char * s) {

	int i,j,n = strlen(s);
	for (i = 0; i <= n/2; i++) {
		
		for (j = 0;s1[j] && s[i] != s1[j]; j++);
		if (s1[j])
			if (s[n-1-i] != s2[j])
				return false;
	}
	return true;
}
int main() {

	
	char s[N];
	bool m, p;
	int i,j;
	while (scanf("%s", &s) == 1) {

		p=ispalindrome(s);
		m=ismirrored(s);
		if(m && p )
			printf("%s -- is a mirrored palindrome.\n",s);
		else if (p)
			printf("%s -- is a regular palindrome.\n",s);
		else if (m)
			printf("%s -- is a mirrored string.\n",s);
		else 
			printf("%s -- is not a palindrome.\n",s);
		printf("\n");
	}
	return 0;
}


阅读更多
个人分类: UVA string
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

uva--401 Palindromes

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭