判断回文串
Problem Description
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。
Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。
Output
如果一个字符串是回文串,则输出"yes",否则输出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
##思路
采用尺取法i=0,j=n-1对相应的字母进行是否相同的判断
代码如下
注:cin和cout是c++里的输入输出,string,和size()也是c++里的,endl是换行
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
char str[1000];//还可以写成string s;
for(int k=0;k<n;k++)//一定要一个大循环,因为要循环几组数据进行一下操作
{
bool ans=true;//写在循环里,每次重新变为true,因为要对每一组进行
cin>>str;
for(int i=0,j=strlen(str)-1;i<j;i++,j--)//strlen()可写成s.size()
{
if(str[i]!=str[j])
{
ans=false;
break;//一个一个判断所以要跳出来
}
}
if(ans==true) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}
仅供参考,欢迎指正。