您的位置:首页 > 其它

C - Palindromes——常量数组运用

2017-03-30 18:45 162 查看
think:

1常量数组运用

2isalpha(char ch)——判断是否为英文字母

Presentation error错误

Hint:In addition, after each output line, you must print an empty line.

Vjudge题目链接

以下为Presentation error代码——每组数据输出后应输出换行

#include <bits/stdc++.h>

using namespace std;

char st[] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";

char tra(char ch)
{
if(isalpha(ch))
return st[ch-'A'];
else
return st[ch-'0'+25];
}

int main()
{
char st1[24];
int i, p, m;
while(scanf("%s", st1) != EOF)
{
int len = strlen(st1);
p = 1, m = 1;
for(i = 0; i < (len+1)/2; i++)
{
if(st1[i] == '0')
st1[i] = 'O';
if(st1[i] != st1[len-1-i])
p = 0;
if(st1[i] != tra(st1[len-1-i]))
m = 0;
}
if(p == 0 && m == 0)
printf("%s -- is not a palindrome.\n", st1);
else if(p == 1 && m == 0)
printf("%s -- is a regular palindrome.\n", st1);
else if(p == 0 && m == 1)
printf("%s -- is a mirrored string.\n", st1);
else
printf("%s -- is a mirrored palindrome.\n", st1);
}
return 0;
}


以下为accepted代码

#include <bits/stdc++.h>

using namespace std;

char st[] = "A   3  HIL JM O   2TUVWXY51SE Z  8 ";

char tra(char ch)
{
if(isalpha(ch))
return st[ch-'A'];
else
return st[ch-'0'+25];
}

int main()
{
char st1[24];
int i, p, m;
while(scanf("%s", st1) != EOF)
{
int len = strlen(st1);
p = 1, m = 1;
for(i = 0; i < (len+1)/2; i++)
{
if(st1[i] == '0')
st1[i] = 'O';
if(st1[i] != st1[len-1-i])
p = 0;
if(st1[i] != tra(st1[len-1-i]))
m = 0;
}
if(p == 0 && m == 0)
printf("%s -- is not a palindrome.\n", st1);
else if(p == 1 && m == 0)
printf("%s -- is a regular palindrome.\n", st1);
else if(p == 0 && m == 1)
printf("%s -- is a mirrored string.\n", st1);
else
printf("%s -- is a mirrored palindrome.\n", st1);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  常量数组运用