UVa 401 Palindromes(例题3-3)
2016-12-01 14:00
417 查看
这个题我提交了三次才过。。。为什么呢?因为在写all数组的时候空格位置不太对,然后就是在msg的某个数组的地方少了一个逗号。。
(细心。。一定要细心。。)
主要的话,还是在镜像串的处理上面,回文串直接枚举中心就行了,回文串用一个数组来替换就行
代码:
#include<cstdio>
#include<cstring>
#include<iostream>.
using namespace std;
const char all[]="A 3 HIL JM O 2TUVWXY51SE Z 8 "; //用于镜像串转换
char s[100];
const char* msg[] = {" -- is not a palindrome."," -- is a regular palindrome.",
" -- is a mirrored string."," -- is a mirrored palindrome."};
char turn(char c) //判断是否数字 ,书上用了isalpha()函数,但是反面直接判断数字也可以
{
if(c>='0'&&c<='9') return all[25+c-'0'];
else return all[c-'A'];
}
int main()
{
while((scanf("%s",s))==1)
{
int len = strlen(s);
int m = 1,n = 1;
for(int i = 0;i<(len+1)/2;i++)
{
if(s[i]!=s[len-i-1]) m = 0;
if(turn(s[i])!=s[len-i-1]) n = 0;
}
cout<<s<<msg[n*2+m]<<endl<<endl;
}
return 0;
}
(细心。。一定要细心。。)
主要的话,还是在镜像串的处理上面,回文串直接枚举中心就行了,回文串用一个数组来替换就行
代码:
#include<cstdio>
#include<cstring>
#include<iostream>.
using namespace std;
const char all[]="A 3 HIL JM O 2TUVWXY51SE Z 8 "; //用于镜像串转换
char s[100];
const char* msg[] = {" -- is not a palindrome."," -- is a regular palindrome.",
" -- is a mirrored string."," -- is a mirrored palindrome."};
char turn(char c) //判断是否数字 ,书上用了isalpha()函数,但是反面直接判断数字也可以
{
if(c>='0'&&c<='9') return all[25+c-'0'];
else return all[c-'A'];
}
int main()
{
while((scanf("%s",s))==1)
{
int len = strlen(s);
int m = 1,n = 1;
for(int i = 0;i<(len+1)/2;i++)
{
if(s[i]!=s[len-i-1]) m = 0;
if(turn(s[i])!=s[len-i-1]) n = 0;
}
cout<<s<<msg[n*2+m]<<endl<<endl;
}
return 0;
}
相关文章推荐
- 字符串与数组03-例题3 Palindromes,UVa401
- 章三例题3——UVA 401 Palindromes
- 章三例题3——UVA 401 Palindromes
- 章三例题3——UVA 401 Palindromes
- 章三例题3——UVA 401 Palindromes
- 【例题3-3 UVA - 401】Palindromes
- 章三例题3——UVA 401 Palindromes
- 章三例题3——UVA 401 Palindromes
- 章三例题3——UVA 401 Palindromes
- 章三例题3——UVA 401 Palindromes
- UVa 401 - Palindromes
- 例题3-3 回文词 UVa401
- UVA 401 Palindromes
- UVa 401 - Palindromes
- UVa 401 - Palindromes
- UVA 401 - Palindromes
- uva 401 - Palindromes
- HDU1318 POJ1590 UVA401 ZOJ1325 Palindromes【回文+水题】
- uva-401 - Palindromes
- UVa 401 Palindromes(镜像回文字符串)