簡單迴文判斷
“迴文串”是一個正讀和反讀都一樣的字符串,比如“level”或者“noon”等等就是迴文串。請寫一個程序判斷讀入的字符串是否是“迴文”.輸入包含多個測試實例,輸入數據的第一行是一個正整數n,表示測試實例的個數,後面緊跟着是n個字符串.如果一個字符串是迴文串,則輸出"yes",否則輸出"no".
sample input:
4
level
abcde
noon
haha
sample output:
yes
no
yes
no
這個程序第一次運行的時候爆CE,當時選的編譯環境是c++,然後調成g++就AC了。
c++編譯會顯示:
這其實來自於頭文件的問題,如果想要在標準c++情況下編譯通過,頭文件改成 #include< string >
然後對於字符串的處理,g++和c++也是有一定的區別的,一個日常的例子就是gcc中gets和puts不能使用,我們可以用fgets來代替gets,fgets常用方式: fgets(a,100,stdin)//stdin表示接收鍵盤輸入。
在c++中我們接收字符串可以使用cin.getline(接收字符串的變量,接收字符個數,結束字符),第三個參數省略是系統自動默認爲"\0".
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
bool judge(string str)
{
int begin=0;
int end=str.size()-1;
while(begin<end)
{
if(str[begin]!=str[end]) return false;
begin++;
end--;
}
}
int main()
{
int n;
cin>>n;
while(n--)
{
string str;
cin>>str;
if(judge(str)) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
return 0;
}