如“XYZYX”和“xyzzyx”都是回文。
函数接口定义:
bool palindrome( char *s );
函数palindrome
判断输入字符串char *s
是否为回文。若是则返回true
,否则返回false
。
裁判测试程序样例:
#include <stdio.h>
#include <string.h>
#define MAXN 20
typedef enum {false, true} bool;
bool palindrome( char *s );
int main() { char s[MAXN]; scanf("%s", s);
if ( palindrome(s)==true ) printf("Yes\n");
else printf("No\n"); printf("%s\n", s); return 0; }
/* 你的代码将被嵌在这里 */
输入样例1:
thisistrueurtsisiht
输出样例1:
Yes
thisistrueurtsisiht
输入样例2:
thisisnottrue
输出样例2:
No
thisisnottrue
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
bool palindrome( char *s )
{
int right=strlen(s)-1;
int left=0;
while(left<right)
{
if(*(s+left)!=*(s+right))
return false;
left++;
right--;
}
return true;
这种根据左右下标来做的题目都是一个道理
左边定义为0 右边定义为字符串长度-1 每次循环后左++右--