ACM的C语言题目,求高手搭救!

Description
回文串是从左到右或者从右到左读起来都一样的字符串,试编程判别一个字符串是否为回文串

Input
输入一个字符串。串长度<255.

Output
判别输入的字符串是否为回文串,是输出"Y",否则输出"N"。

Sample Input
abcba

Sample Output
Y
HINT

如果你只输入一个字符串,可以把程序里的while循环改了,不过对于ACM程序的判定来说,输入一个也是可以用while来进行输入的,你可以先提交一下我的代码试试。

count函数用来判定字符串s是否是回文,如果是返回1,否则返回0.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Count(char *s)
{
int i, j, t;
t = strlen(s);
i=0, j=t-1;
while(i<j)
{
if(s[i]!=s[j])
{
return 0;
}
i++, j--;
}
return 1;
}
int main()
{
char s[280];
while(scanf("%s", s)==1)
{
if(Count(s)==1)
{
putchar('Y');
}
else
{
putchar('N');
}
putchar('\n');
}
return 0;
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2013-04-24
#include(stdio.h)
int str_len(char *p)
{
int n=0;
while(*p!='\0')
{
n++;
p++;
}
return n;
}
int main()
{
char a[255];
int n,i;
printf("输入一个字符串:\n");
gets(a);
n=str_len(a);
for(i=0;i<n/2;i++){
if(a[i]!=a[n-1]){
printf("N\n");
return 0;
}
}
printf("Y\n");
return 1;
}

第2个回答  2013-04-24
#include<iostream>
using namespace std;
int test(char d[],int k,int i)
{
if(d[k]==d[i-k]&&k==0)
return 1;
else if(d[k]==d[i-k])
test(d,k-1,i);
else
return 0;
}
void main()
{
int i=0;n=0;
char b[255];
while((a=getchar())!=\n)
{
b[i]=a;
i++;
}
if(i%2==0)
n=test(b,(i/2),i-1);
else
n=test(b,(i/2-1),i-1);
if(n==0)
cout<<"N";
else
cout<<"Y";

}

相似回答