OJ地址:https://vjudge.net/problem/HDU-2029
“迴文串”是一個正讀和反讀都一樣的字符串,比如“level”或者“noon”等等就是迴文串。請寫一個程序判斷讀入的字符串是否是“迴文”。
Input
輸入包含多個測試實例,輸入數據的第一行是一個正整數n,表示測試實例的個數,後面緊跟着是n個字符串。
Output
如果一個字符串是迴文串,則輸出"yes",否則輸出"no".
Sample Input
4
level
abcde
noon
haha
Sample Output
yes
no
yes
no
思路:
拿第一個元素與最後一個元素比較,拿第二個元素和倒數第二個元素比較......,循環到字符串中間位置就可以結束,降低時間複雜度。
程序代碼:
#include<cstdio>
#include<cstring>
int main(){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
char str[1001];
scanf("%s",str);
int x=strlen(str);
int flag = 0;
for(int i=0;i<x/2;i++){
if(str[i]==str[x-i-1]){
continue;
}else{
flag=1;
break;
}
}
if(flag==1)
printf("no\n");
else
printf("yes\n");
}
return 0;
}