
一道ACM简单题
http://acm.tju.edu.cn/toj/showp1539.html这是题目的网址这个是我写的代码:#include<iostream>//a=97,A=65...
http://acm.tju.edu.cn/toj/showp1539.html这是题目的网址
这个是我写的代码:
#include <iostream>//a=97,A=65,,97-122,65-90
#include <string.h>
using namespace std;
int main(){
char s[88];
while(cin.getline(s,88)){
if(s[0]=='T'&&s[1]=='H'&&s[2]=='E'&&s[3]==' '&&s[4]=='E'&&s[5]=='N'&&s[6]=='D'&&s[7]=='.')break;
int high=strlen(s)-1,low=0;
bool flag=1;
while(high>low){
while(s[low]>122||s[low]<65||(90<s[low]&&s[low]<97))low++;
while(s[high]>122||s[high]<65||(90<s[high]&&s[high]<97))high--;
if(s[low]-s[high]==0||s[low]-s[high]==32||s[high]-s[low]==32)low++,high--;
else {flag=0;break;}
}
if(flag)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
测试了下数据感觉上是对的。我的理解是英语字母之外的字符不会对结果产生影响,是这里理解错了吗?还是其它的原因。 展开
这个是我写的代码:
#include <iostream>//a=97,A=65,,97-122,65-90
#include <string.h>
using namespace std;
int main(){
char s[88];
while(cin.getline(s,88)){
if(s[0]=='T'&&s[1]=='H'&&s[2]=='E'&&s[3]==' '&&s[4]=='E'&&s[5]=='N'&&s[6]=='D'&&s[7]=='.')break;
int high=strlen(s)-1,low=0;
bool flag=1;
while(high>low){
while(s[low]>122||s[low]<65||(90<s[low]&&s[low]<97))low++;
while(s[high]>122||s[high]<65||(90<s[high]&&s[high]<97))high--;
if(s[low]-s[high]==0||s[low]-s[high]==32||s[high]-s[low]==32)low++,high--;
else {flag=0;break;}
}
if(flag)cout<<"Yes\n";
else cout<<"No\n";
}
return 0;
}
测试了下数据感觉上是对的。我的理解是英语字母之外的字符不会对结果产生影响,是这里理解错了吗?还是其它的原因。 展开
2个回答
展开全部
A palindrome is a word or phrase which is spelled the same backwards and
forwards (ignoring any punctuation and whitespace).
如果是数字的话,那你的应该就有问题,你把数字加进去吧。
给个建议。。。不用直接写成ASCII吗。。。
直接 用 s[low]<'A' 这些。。这样别人看的懂。
判断退出条件,直接用字符串。。。不用一个一个等于。。。
strcmp 字符串比较函数 。
forwards (ignoring any punctuation and whitespace).
如果是数字的话,那你的应该就有问题,你把数字加进去吧。
给个建议。。。不用直接写成ASCII吗。。。
直接 用 s[low]<'A' 这些。。这样别人看的懂。
判断退出条件,直接用字符串。。。不用一个一个等于。。。
strcmp 字符串比较函数 。

2024-10-28 广告
作为上海华然企业咨询有限公司的一员,我们深知大模型测试对于企业数字化转型与智能决策的重要性。在应对此类测试时,我们注重数据的精准性、算法的先进性及模型的适用性,确保大模型能够精准捕捉市场动态,高效分析企业数据,为管理层提供科学、前瞻的决策支...
点击进入详情页
本回答由上海华然企业咨询提供
展开全部
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
int main() {
char s[88];
int i,j,len,flag;
while(cin.getline(s,88)) {
len = strlen(s);
for(i = 0, j = len - 1; i < j; ++i,--j) {
while(!isalpha(s[i]) && i < j) ++i; // 剔除非alpha字符
while(!isalpha(s[j]) && i < j) --j;
flag = 1;
if(tolower(s[i]) != tolower(s[j])) {
flag = 0;
break;
}
}
if(flag == 1) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询